From 42ba3997d64591822411fbbedd51a240dbbb5fab Mon Sep 17 00:00:00 2001 From: "José M. Guisado" Date: Wed, 18 Sep 2019 13:28:29 +0200 Subject: Check memunit parsing for overflow in confparse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before, when parsing memunits, if overflow occured it failed silently. Use nowrap u64 math to detect overflow, compare to INT64_MAX and if greater tell user and fail accordingly. 15000000.5 TB fails double check as it a greater floating number than (double)INT64_MAX 8388608.1 TB passes double check because it falls in the same value as (double)INT64_MAX (which is 2^63), but will fail the int check because (uint64_t)d, which is 2^63, is strictly greater than 2^63-1 (INT64_MAX). Fixes #30920 Signed-off-by: José M. Guisado --- src/test/test_confparse.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/test/test_confparse.c') diff --git a/src/test/test_confparse.c b/src/test/test_confparse.c index 5f29a22c10..e0c9b3f63b 100644 --- a/src/test/test_confparse.c +++ b/src/test/test_confparse.c @@ -906,11 +906,22 @@ test_confparse_unitparse(void *args) tt_assert(ok); /* u64 overflow */ - /* XXXX our implementation does not currently detect this. See bug 30920. */ - /* tt_u64_op(config_parse_memunit("20000000 TB", &ok), OP_EQ, 0); tt_assert(!ok); - */ + // This test fails the double check as the float representing 15000000.5 TB + // is greater than (double) INT64_MAX + tt_u64_op(config_parse_memunit("15000000.5 TB", &ok), OP_EQ, 0); + tt_assert(!ok); + // 8388608.1 TB passes double check because it falls in the same float + // value as (double)INT64_MAX (which is 2^63) due to precision. + // But will fail the int check because the unsigned representation of + // the float, which is 2^63, is strictly greater than INT64_MAX (2^63-1) + tt_u64_op(config_parse_memunit("8388608.1 TB", &ok), OP_EQ, 0); + tt_assert(!ok); + + /* negative float */ + tt_u64_op(config_parse_memunit("-1.5 GB", &ok), OP_EQ, 0); + tt_assert(!ok); /* i32 overflow */ tt_int_op(config_parse_interval("1000 months", &ok), OP_EQ, -1); -- cgit v1.2.3-54-g00ecf