summaryrefslogtreecommitdiffstats
path: root/parse.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2021-10-01 11:13:12 -0400
committerTavian Barnes <tavianator@tavianator.com>2021-10-01 11:26:11 -0400
commitd1c441d3c80abfa6d6cb877270f200cc4edb6364 (patch)
tree71b7d9240d4784829dabb387f899f1bbaac20b23 /parse.c
parentf1d209d939b21ffc031001a9fddd37466e12fc8d (diff)
downloadbfs-d1c441d3c80abfa6d6cb877270f200cc4edb6364.tar.xz
tests: Add tests for parsing invalid integers
And give some better error messages.
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c24
1 files changed, 20 insertions, 4 deletions
diff --git a/parse.c b/parse.c
index 22c2473..a7ee3ab 100644
--- a/parse.c
+++ b/parse.c
@@ -510,7 +510,11 @@ static const char *parse_int(const struct parser_state *state, const char *str,
errno = 0;
long long value = strtoll(str, &endptr, base);
if (errno != 0) {
- goto bad;
+ if (errno == ERANGE) {
+ goto range;
+ } else {
+ goto bad;
+ }
}
if (endptr == str) {
@@ -522,20 +526,20 @@ static const char *parse_int(const struct parser_state *state, const char *str,
}
if ((flags & IF_UNSIGNED) && value < 0) {
- goto bad;
+ goto negative;
}
switch (flags & IF_SIZE_MASK) {
case IF_INT:
if (value < INT_MIN || value > INT_MAX) {
- goto bad;
+ goto range;
}
*(int *)result = value;
break;
case IF_LONG:
if (value < LONG_MIN || value > LONG_MAX) {
- goto bad;
+ goto range;
}
*(long *)result = value;
break;
@@ -556,6 +560,18 @@ bad:
parse_error(state, "${bld}%s${rs} is not a valid integer.\n", str);
}
return NULL;
+
+negative:
+ if (!(flags & IF_QUIET)) {
+ parse_error(state, "Negative integer ${bld}%s${rs} is not allowed here.\n", str);
+ }
+ return NULL;
+
+range:
+ if (!(flags & IF_QUIET)) {
+ parse_error(state, "${bld}%s${rs} is too large an integer.\n", str);
+ }
+ return NULL;
}
/**