From 9d604959036bd02e7ff0ca1bd33f842664610b82 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 10 Sep 2019 18:59:10 -0400 Subject: Use strtod, not atof, for parsing doubles in the configuration. This lets us detect erroneous doubles, which previously we could not do. Fixes bug 31475; bugfix on commit 00a9e3732e88, a.k.a svn:r136. --- src/test/test_confparse.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/test') diff --git a/src/test/test_confparse.c b/src/test/test_confparse.c index bd2b5cdf1b..a4da6c7c9f 100644 --- a/src/test/test_confparse.c +++ b/src/test/test_confparse.c @@ -488,6 +488,8 @@ test_confparse_assign_badval(void *arg) static const badval_test_t bv_notint = { "pos X\n", "malformed" }; static const badval_test_t bv_negint = { "pos -10\n", "out of bounds" }; static const badval_test_t bv_badu64 = { "u64 u64\n", "malformed" }; +static const badval_test_t bv_dbl1 = { "dbl xxx\n", "Could not convert" }; +static const badval_test_t bv_dbl2 = { "dbl 1.0 xx\n", "Could not convert" }; static const badval_test_t bv_badcsvi1 = { "csv_interval 10 wl\n", "malformed" }; static const badval_test_t bv_badcsvi2 = @@ -1045,6 +1047,8 @@ struct testcase_t confparse_tests[] = { BADVAL_TEST(notint), BADVAL_TEST(negint), BADVAL_TEST(badu64), + BADVAL_TEST(dbl1), + BADVAL_TEST(dbl2), BADVAL_TEST(badcsvi1), BADVAL_TEST(badcsvi2), BADVAL_TEST(nonoption), -- cgit v1.2.3-54-g00ecf From bfc5f09979d49867b373b9433edf37adce8c66dd Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 13 Sep 2019 18:24:15 -0400 Subject: Detect overflow or underflow on double config values. Any floating point value too positive or negative to distinguish from +/-Inf, or too small to distinguish from +/-0, is an over/underflow. --- src/lib/confmgt/type_defs.c | 13 +++++++++++-- src/test/test_confparse.c | 12 ++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) (limited to 'src/test') diff --git a/src/lib/confmgt/type_defs.c b/src/lib/confmgt/type_defs.c index 137af4ed9e..6b0eac7823 100644 --- a/src/lib/confmgt/type_defs.c +++ b/src/lib/confmgt/type_defs.c @@ -37,6 +37,7 @@ #include #include +#include ////// // CONFIG_TYPE_STRING @@ -284,15 +285,23 @@ double_parse(void *target, const char *value, char **errmsg, (void)errmsg; double *v = (double*)target; char *endptr=NULL; + errno = 0; *v = strtod(value, &endptr); if (endptr == value || *endptr != '\0') { // Either there are no converted characters, or there were some characters // that didn't get converted. tor_asprintf(errmsg, "Could not convert %s to a number.", escaped(value)); return -1; - } else { - return 0; } + if (errno == ERANGE) { + // strtod will set errno to ERANGE on underflow or overflow. + bool underflow = -.00001 < *v && *v < .00001; + tor_asprintf(errmsg, + "%s is too %s to express as a floating-point number.", + escaped(value), underflow ? "small" : "large"); + return -1; + } + return 0; } static char * diff --git a/src/test/test_confparse.c b/src/test/test_confparse.c index a4da6c7c9f..f04853af72 100644 --- a/src/test/test_confparse.c +++ b/src/test/test_confparse.c @@ -490,6 +490,14 @@ static const badval_test_t bv_negint = { "pos -10\n", "out of bounds" }; static const badval_test_t bv_badu64 = { "u64 u64\n", "malformed" }; static const badval_test_t bv_dbl1 = { "dbl xxx\n", "Could not convert" }; static const badval_test_t bv_dbl2 = { "dbl 1.0 xx\n", "Could not convert" }; +static const badval_test_t bv_dbl3 = { + "dbl 1e-10000\n", "too small to express" }; +static const badval_test_t bv_dbl4 = { + "dbl 1e1000\n", "too large to express" }; +static const badval_test_t bv_dbl5 = { + "dbl -1e-10000\n", "too small to express" }; +static const badval_test_t bv_dbl6 = { + "dbl -1e1000\n", "too large to express" }; static const badval_test_t bv_badcsvi1 = { "csv_interval 10 wl\n", "malformed" }; static const badval_test_t bv_badcsvi2 = @@ -1049,6 +1057,10 @@ struct testcase_t confparse_tests[] = { BADVAL_TEST(badu64), BADVAL_TEST(dbl1), BADVAL_TEST(dbl2), + BADVAL_TEST(dbl3), + BADVAL_TEST(dbl4), + BADVAL_TEST(dbl5), + BADVAL_TEST(dbl6), BADVAL_TEST(badcsvi1), BADVAL_TEST(badcsvi2), BADVAL_TEST(nonoption), -- cgit v1.2.3-54-g00ecf