summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Kadianakis <desnacked@riseup.net>2019-09-16 15:22:18 +0300
committerGeorge Kadianakis <desnacked@riseup.net>2019-09-16 15:22:18 +0300
commitcd72850e08f39413d281a480a78f38838ddf42e9 (patch)
treefb7177a1107c50605c74741de99d97f5474089b4
parent5ec751b38b6ea5b40dece321a2c673c6b7e9cf01 (diff)
parentbfc5f09979d49867b373b9433edf37adce8c66dd (diff)
downloadtor-cd72850e08f39413d281a480a78f38838ddf42e9.tar.gz
tor-cd72850e08f39413d281a480a78f38838ddf42e9.zip
Merge branch 'tor-github/pr/1316'
-rw-r--r--changes/ticket314755
-rw-r--r--src/lib/confmgt/type_defs.c20
-rw-r--r--src/test/test_confparse.c16
3 files changed, 39 insertions, 2 deletions
diff --git a/changes/ticket31475 b/changes/ticket31475
new file mode 100644
index 0000000000..e156c145a9
--- /dev/null
+++ b/changes/ticket31475
@@ -0,0 +1,5 @@
+ o Minor bugfixes (configuration):
+ - Invalid floating-point values in the configuration file are now
+ detected treated as errors in the configuration. Previously, they
+ were ignored and treated as zero. Fixes bug 31475; bugfix on
+ 0.0.1.
diff --git a/src/lib/confmgt/type_defs.c b/src/lib/confmgt/type_defs.c
index 324b62e56c..62c12fcddd 100644
--- a/src/lib/confmgt/type_defs.c
+++ b/src/lib/confmgt/type_defs.c
@@ -37,6 +37,7 @@
#include <stddef.h>
#include <string.h>
+#include <errno.h>
//////
// CONFIG_TYPE_STRING
@@ -283,8 +284,23 @@ double_parse(void *target, const char *value, char **errmsg,
(void)params;
(void)errmsg;
double *v = (double*)target;
- // XXXX This is the preexisting behavior, but we should detect errors here.
- *v = atof(value);
+ 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;
+ }
+ 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;
}
diff --git a/src/test/test_confparse.c b/src/test/test_confparse.c
index d929d1e361..5f29a22c10 100644
--- a/src/test/test_confparse.c
+++ b/src/test/test_confparse.c
@@ -488,6 +488,16 @@ 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_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 =
@@ -1045,6 +1055,12 @@ struct testcase_t confparse_tests[] = {
BADVAL_TEST(notint),
BADVAL_TEST(negint),
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),