diff options
author | José M. Guisado <guigom@riseup.net> | 2019-09-18 13:28:29 +0200 |
---|---|---|
committer | José M. Guisado <guigom@riseup.net> | 2019-10-28 11:38:41 +0100 |
commit | 42ba3997d64591822411fbbedd51a240dbbb5fab (patch) | |
tree | 3845879d4c2914f2ef22c9019ac90da31ebc8462 /src/test | |
parent | f237529fff657bbbf8d2f2632337d9848446d7da (diff) | |
download | tor-42ba3997d64591822411fbbedd51a240dbbb5fab.tar.gz tor-42ba3997d64591822411fbbedd51a240dbbb5fab.zip |
Check memunit parsing for overflow in confparse
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 <guigom@riseup.net>
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test_confparse.c | 17 |
1 files changed, 14 insertions, 3 deletions
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); |