In a gnulib testdir, with a gcc 15 snapshot, I see these warnings: ../../gltests/test-getlogin.c:45:11: warning: passing argument 1 of 'putenv' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers] ../../gltests/test-getlogin_r.c:72:11: warning: passing argument 1 of 'putenv' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
These warnings point to bugs in the tests: A string passed to putenv() gets included in 'environ' and therefore must be writable. But literal strings are typically allocated in read-only memory. These two patches should fix it. 2025-04-13 Bruno Haible <br...@clisp.org> getlogin_r tests: Avoid writing to a literal string. * tests/test-getlogin_r.c: Include xalloc.h. (main): Pass a writable string to putenv(). * modules/getlogin_r-tests (Depends-on): Add xalloc. (Makefile.am): Link the program with libintl when needed. getlogin tests: Avoid writing to a literal string. * tests/test-getlogin.c: Include xalloc.h. (main): Pass a writable string to putenv(). * modules/getlogin-tests (Depends-on): Add xalloc. (Makefile.am): Link the program with libintl when needed.
>From 9291c5c31a7dd325e7366e5db777c0b4882b9abd Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 13 Apr 2025 23:17:06 +0200 Subject: [PATCH 1/2] getlogin tests: Avoid writing to a literal string. * tests/test-getlogin.c: Include xalloc.h. (main): Pass a writable string to putenv(). * modules/getlogin-tests (Depends-on): Add xalloc. (Makefile.am): Link the program with libintl when needed. --- ChangeLog | 8 ++++++++ modules/getlogin-tests | 3 ++- tests/test-getlogin.c | 5 ++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index c766cc9cca..1547bed51e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2025-04-13 Bruno Haible <br...@clisp.org> + + getlogin tests: Avoid writing to a literal string. + * tests/test-getlogin.c: Include xalloc.h. + (main): Pass a writable string to putenv(). + * modules/getlogin-tests (Depends-on): Add xalloc. + (Makefile.am): Link the program with libintl when needed. + 2025-04-13 Bruno Haible <br...@clisp.org> vasnprintf: Optimize last commit. diff --git a/modules/getlogin-tests b/modules/getlogin-tests index 8637b2ea7e..6484ffe251 100644 --- a/modules/getlogin-tests +++ b/modules/getlogin-tests @@ -6,10 +6,11 @@ tests/macros.h Depends-on: bool +xalloc configure.ac: Makefile.am: TESTS += test-getlogin check_PROGRAMS += test-getlogin -test_getlogin_LDADD = $(LDADD) $(GETLOGIN_LIB) +test_getlogin_LDADD = $(LDADD) @LIBINTL@ $(GETLOGIN_LIB) diff --git a/tests/test-getlogin.c b/tests/test-getlogin.c index 431844bd71..11f67de542 100644 --- a/tests/test-getlogin.c +++ b/tests/test-getlogin.c @@ -18,11 +18,14 @@ #include <config.h> +/* Specification. */ #include <unistd.h> #include "signature.h" SIGNATURE_CHECK (getlogin, char *, (void)); +#include "xalloc.h" + #include "test-getlogin.h" int @@ -42,7 +45,7 @@ main (void) test_getlogin_result (buf, err); /* Check that getlogin() does not merely return getenv ("LOGNAME"). */ - putenv ("LOGNAME=ygvfibmslhkmvoetbrcegzwydorcke"); + putenv (xstrdup ("LOGNAME=ygvfibmslhkmvoetbrcegzwydorcke")); buf = getlogin (); ASSERT (!(buf != NULL && strcmp (buf, "ygvfibmslhkmvoetbrcegzwydorcke") == 0)); -- 2.43.0
>From ebff098c2e7252ffc4c926eb33a87bba9c035e3c Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 13 Apr 2025 23:18:10 +0200 Subject: [PATCH 2/2] getlogin_r tests: Avoid writing to a literal string. * tests/test-getlogin_r.c: Include xalloc.h. (main): Pass a writable string to putenv(). * modules/getlogin_r-tests (Depends-on): Add xalloc. (Makefile.am): Link the program with libintl when needed. --- ChangeLog | 6 ++++++ modules/getlogin_r-tests | 3 ++- tests/test-getlogin_r.c | 5 ++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1547bed51e..2ea548d13f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2025-04-13 Bruno Haible <br...@clisp.org> + getlogin_r tests: Avoid writing to a literal string. + * tests/test-getlogin_r.c: Include xalloc.h. + (main): Pass a writable string to putenv(). + * modules/getlogin_r-tests (Depends-on): Add xalloc. + (Makefile.am): Link the program with libintl when needed. + getlogin tests: Avoid writing to a literal string. * tests/test-getlogin.c: Include xalloc.h. (main): Pass a writable string to putenv(). diff --git a/modules/getlogin_r-tests b/modules/getlogin_r-tests index 8fefa059bc..3cddb65584 100644 --- a/modules/getlogin_r-tests +++ b/modules/getlogin_r-tests @@ -6,10 +6,11 @@ tests/macros.h Depends-on: bool +xalloc configure.ac: Makefile.am: TESTS += test-getlogin_r check_PROGRAMS += test-getlogin_r -test_getlogin_r_LDADD = $(LDADD) $(GETLOGIN_LIB) +test_getlogin_r_LDADD = $(LDADD) @LIBINTL@ $(GETLOGIN_LIB) diff --git a/tests/test-getlogin_r.c b/tests/test-getlogin_r.c index 3674195e55..e27e23da23 100644 --- a/tests/test-getlogin_r.c +++ b/tests/test-getlogin_r.c @@ -18,6 +18,7 @@ #include <config.h> +/* Specification. */ #include <unistd.h> #include "signature.h" @@ -25,6 +26,8 @@ SIGNATURE_CHECK (getlogin_r, int, (char *, size_t)); #endif +#include "xalloc.h" + #include "test-getlogin.h" int @@ -69,7 +72,7 @@ main (void) } /* Check that getlogin_r() does not merely return getenv ("LOGNAME"). */ - putenv ("LOGNAME=ygvfibmslhkmvoetbrcegzwydorcke"); + putenv (xstrdup ("LOGNAME=ygvfibmslhkmvoetbrcegzwydorcke")); err = getlogin_r (buf, sizeof buf); ASSERT (!(err == 0 && strcmp (buf, "ygvfibmslhkmvoetbrcegzwydorcke") == 0)); -- 2.43.0