summaryrefslogtreecommitdiff
path: root/src/lib/confmgt
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2019-09-13 18:24:15 -0400
committerNick Mathewson <nickm@torproject.org>2019-09-13 18:26:16 -0400
commitbfc5f09979d49867b373b9433edf37adce8c66dd (patch)
tree516ce31d25f082710cd511cd61e549f132566b60 /src/lib/confmgt
parent9d604959036bd02e7ff0ca1bd33f842664610b82 (diff)
downloadtor-bfc5f09979d49867b373b9433edf37adce8c66dd.tar.gz
tor-bfc5f09979d49867b373b9433edf37adce8c66dd.zip
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.
Diffstat (limited to 'src/lib/confmgt')
-rw-r--r--src/lib/confmgt/type_defs.c13
1 files changed, 11 insertions, 2 deletions
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 <stddef.h>
#include <string.h>
+#include <errno.h>
//////
// 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 *