Tests of undefined behaviour are tricky to get right.
A testdir of module 'xstrtol', created through
$ ./gnulib-tool --create-testdir --dir=../testdir xstrtol
fails on several platforms: musl libc, macOS, NetBSD, OpenBSD, Solaris, mingw.
The failure looks like this:
$ gltests/test-xstrtol
../../gnulib-tests/test-xstrtol.c:82: assertion 's_err == LONGINT_INVALID'
failed
How come?
* Module 'xstrtol' does not depend on module 'strtol'.
Therefore module 'strtol' is not built in gllib/.
* But module 'strtol' is used in gltests/, due to this dependency chain:
xstrtol-tests -> xstrtol-error -> error -> stdio-h
=> stdio-h-tests -> signed-nan
=>
signed-nan-tests -> strtol
* configure finds
checking whether strtol works... no
which is due to a 2023-03-16 commit:
"strtol, strtoll, strtoul, strtoull: Make ISO C 23 compliant."
All this is correct. But the effect is that test-xstrtol.c, when compiled in
gltests/,
assumes that the Gnulib replacement of strtol is in effect, which it isn't.
This patch avoids the test failure.
2026-04-25 Bruno Haible <[email protected]>
xstrtol tests: Don't rely on is_GNULIB_strtol.
In a testdir, the Gnulib replacement for strtol may be used in gltests/
but not in gllib/.
* tests/test-xstrtol.c (is_GNULIB_strtol): Remove macro.
(main): Accept both behaviours of strtol on all platforms except MSVC.
* tests/test-xstrtoll.c (is_GNULIB_strtol): Remove macro.
diff --git a/tests/test-xstrtol.c b/tests/test-xstrtol.c
index 108aa20fd2..6c20c5b8d2 100644
--- a/tests/test-xstrtol.c
+++ b/tests/test-xstrtol.c
@@ -28,7 +28,6 @@
# define __xstrtol xstrtol
# define __strtol_t long int
# define __spec "ld"
-# define is_GNULIB_strtol GNULIB_defined_strtol_function
#endif
/* Don't show the program name in error messages. */
@@ -68,20 +67,32 @@ main (int argc, char **argv)
/* Test an invalid base (undefined behaviour, as documented in
xstrtol.h).
Reported by Alejandro Colomar. */
-#if !(defined __CYGWIN__ || defined _MSC_VER)
+#if !defined _MSC_VER
{
const char input[] = "k";
char *endp = NULL;
__strtol_t val = -17;
strtol_error s_err = __xstrtol (input, &endp, -1, &val, "k");
-# if !(defined __GLIBC__ || is_GNULIB_strtol)
- ASSERT (s_err == LONGINT_OK);
- ASSERT (endp == input + 1);
- ASSERT (val == 1024);
-# else
- ASSERT (s_err == LONGINT_INVALID);
- ASSERT (val == -17);
-# endif
+ if (s_err == LONGINT_INVALID)
+ {
+ /* On glibc or when the gnulib replacement function is used,
+ strtol (input, &endp, -1) returns 0 with errno == EINVAL,
+ *without* changing endp. xstrtol then returns LONGINT_INVALID.
+ */
+ ASSERT (s_err == LONGINT_INVALID);
+ ASSERT (val == -17);
+ }
+ else
+ {
+ /* On musl libc, macOS, NetBSD, OpenBSD, Solaris, mingw,
+ when the gnulib replacement function is not used,
+ strtol (input, &endp, -1) returns 0 with errno == EINVAL,
+ setting endp = input. xstrtol then performs the suffix
+ processing and finally returns LONGINT_OK. */
+ ASSERT (s_err == LONGINT_OK);
+ ASSERT (endp == input + 1);
+ ASSERT (val == 1024);
+ }
}
#endif
diff --git a/tests/test-xstrtoll.c b/tests/test-xstrtoll.c
index 8da0e3bed1..47a552ebff 100644
--- a/tests/test-xstrtoll.c
+++ b/tests/test-xstrtoll.c
@@ -1,5 +1,4 @@
#define __xstrtol xstrtoll
#define __strtol_t long long int
#define __spec "lld"
-#define is_GNULIB_strtol GNULIB_defined_strtoll_function
#include "test-xstrtol.c"