From d1c441d3c80abfa6d6cb877270f200cc4edb6364 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 1 Oct 2021 11:13:12 -0400 Subject: tests: Add tests for parsing invalid integers And give some better error messages. --- parse.c | 24 ++++++++++++++++++++---- tests.sh | 21 +++++++++++++++++++++ 2 files changed, 41 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; } /** diff --git a/tests.sh b/tests.sh index bfbbf9d..01b5a42 100755 --- a/tests.sh +++ b/tests.sh @@ -738,6 +738,11 @@ bfs_tests=( test_hidden test_hidden_root + test_links_noarg + test_links_empty + test_links_negative + test_links_invalid + test_newerma_nonexistent test_newermt_invalid test_newermq @@ -1348,6 +1353,22 @@ function test_links_minus() { bfs_diff links -type f -links -2 } +function test_links_noarg() { + ! quiet invoke_bfs links -links +} + +function test_links_empty() { + ! quiet invoke_bfs links -links '' +} + +function test_links_negative() { + ! quiet invoke_bfs links -links +-1 +} + +function test_links_invalid() { + ! quiet invoke_bfs links -links ASDF +} + function test_P() { bfs_diff -P links/deeply/nested/dir } -- cgit v1.2.3