Alejandro Colomar wrote: > > > Assuming that the string matches a valid suffix, it'd succeed, but the > > > call should have failed early. > > > > No, because that code is dealing with the case where the number's text is > > missing. And if the number's text is missing it doesn't matter what the base > > is; there are no digits to multiply the base by. > > I think you didn't understand me. I'll show line-by-line where I think > it will go through. > > Assume I call > > xstrtol("k", &end, -1, ...);
This discussion is going nowhere, I think, unless we actually test things. I therefore added your test case to the test suite, assuming the results on a glibc system — patch below — and triggered a CI build. We will know in two hours whether we need to adjust the test case for *BSD systems. 2024-07-25 Bruno Haible <br...@clisp.org> xstrtol, xstrtoll tests: Test behaviour for an invalid base. Reported by Alejandro Colomar <a...@kernel.org> in <https://lists.gnu.org/archive/html/bug-gnulib/2024-07/msg00249.html>. * modules/xstrtol-tests (Files): Add tests/macros.h. * modules/xstrtoll-tests (Files): Likewise. * tests/test-xstrtol.c: Include macros.h. (main): If no arguments are given on the command line, perform miscellaneous tests. * tests/test-xstrtol.sh: Also invoke test-xstrtol without arguments. * tests/test-xstrtoll.sh: Also invoke test-xstrtoll without arguments. diff --git a/modules/xstrtol-tests b/modules/xstrtol-tests index 1786873d07..c52e1611d8 100644 --- a/modules/xstrtol-tests +++ b/modules/xstrtol-tests @@ -2,6 +2,7 @@ Files: tests/test-xstrtol.c tests/test-xstrtoul.c tests/test-xstrtol.sh +tests/macros.h Depends-on: inttypes diff --git a/modules/xstrtoll-tests b/modules/xstrtoll-tests index d4e24d9d9a..d28ebca73f 100644 --- a/modules/xstrtoll-tests +++ b/modules/xstrtoll-tests @@ -3,6 +3,7 @@ tests/test-xstrtol.c tests/test-xstrtoll.c tests/test-xstrtoull.c tests/test-xstrtoll.sh +tests/macros.h Depends-on: inttypes diff --git a/tests/test-xstrtol.c b/tests/test-xstrtol.c index c9568379ae..f83d41905f 100644 --- a/tests/test-xstrtol.c +++ b/tests/test-xstrtol.c @@ -21,6 +21,7 @@ #include <stdio.h> #include <error.h> +#include "macros.h" #include "xstrtol-error.h" #ifndef __xstrtol @@ -38,25 +39,45 @@ print_no_progname (void) int main (int argc, char **argv) { - strtol_error s_err; - int i; - - error_print_progname = print_no_progname; - - for (i = 1; i < argc; i++) + if (argc > 1) { - char *p; - __strtol_t val; + /* Test cases given on the command line. */ + int i; - s_err = __xstrtol (argv[i], &p, 0, &val, "bckMw0"); - if (s_err == LONGINT_OK) - { - printf ("%s->%" __spec " (%s)\n", argv[i], val, p); - } - else + error_print_progname = print_no_progname; + + for (i = 1; i < argc; i++) { - xstrtol_fatal (s_err, -2, 'X', NULL, argv[i]); + char *p; + __strtol_t val; + strtol_error s_err = __xstrtol (argv[i], &p, 0, &val, "bckMw0"); + if (s_err == LONGINT_OK) + { + printf ("%s->%" __spec " (%s)\n", argv[i], val, p); + } + else + { + xstrtol_fatal (s_err, -2, 'X', NULL, argv[i]); + } } + + return 0; + } + else + { + /* Miscellaneous test cases. */ + + /* Test an invalid base. Reported by Alejandro Colomar. */ + { + const char input[] = "k"; + char *endp = NULL; + __strtol_t val = -17; + strtol_error s_err = __xstrtol (input, &endp, -1, &val, "k"); + ASSERT (s_err == LONGINT_INVALID); + ASSERT (endp == NULL); + ASSERT (val == -17); + } + + return test_exit_status; } - exit (0); } diff --git a/tests/test-xstrtol.sh b/tests/test-xstrtol.sh index e82b585bf4..cec0a30764 100755 --- a/tests/test-xstrtol.sh +++ b/tests/test-xstrtol.sh @@ -68,4 +68,7 @@ EOF compare expected out || result=1 +# Other misc tests. +${CHECKER} test-xstrtol || result=1 + Exit $result diff --git a/tests/test-xstrtoll.sh b/tests/test-xstrtoll.sh index d738acc381..b8d235adce 100755 --- a/tests/test-xstrtoll.sh +++ b/tests/test-xstrtoll.sh @@ -64,4 +64,7 @@ EOF compare expected out || result=1 +# Other misc tests. +${CHECKER} test-xstrtoll || result=1 + Exit $result