]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
posix: Move environ helper variables next to environ definition (bug 32541)
authorFlorian Weimer <fweimer@redhat.com>
Fri, 7 Mar 2025 20:40:55 +0000 (21:40 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Fri, 7 Mar 2025 20:40:55 +0000 (21:40 +0100)
This helps with statically interposing getenv.

Updates commit 7a61e7f557a97ab597d6fca5e2d1f13f65685c61
("stdlib: Make getenv thread-safe in more cases").

Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
posix/environ.c
stdlib/Makefile
stdlib/getenv.c
stdlib/tst-getenv-static.c [new file with mode: 0644]

index a0ed0d80eab207f8d0b4b5b01dfaa653c1e4cefd..924effe3cd65dc6903aa031b83876ebbdc88257d 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <unistd.h>
 #include <stddef.h>
+#include <stdlib/setenv.h>
 
 /* This must be initialized; we cannot have a weak alias into bss.  */
 char **__environ = NULL;
@@ -10,3 +11,6 @@ weak_alias (__environ, environ)
 /* The SVR4 ABI says `_environ' will be the name to use
    in case the user overrides the weak alias `environ'.  */
 weak_alias (__environ, _environ)
+
+struct environ_array *__environ_array_list;
+environ_counter __environ_counter;
index 1c4fa2382fd7ea960c9978bce4e1b02d4cead6da..c9c8f702a22bbeb76a78a0d5029c5cd49913fd2e 100644 (file)
@@ -282,6 +282,7 @@ tests := \
   tst-environ-change-3 \
   tst-environ-change-4 \
   tst-getenv-signal \
+  tst-getenv-static \
   tst-getenv-thread \
   tst-getenv-unsetenv \
   tst-getrandom \
@@ -377,6 +378,7 @@ tests-internal := \
   # tests-internal
 
 tests-static := \
+  tst-getenv-static \
   tst-secure-getenv \
   # tests-static
 
index 5e7212cca6f660bf1f5ac93260f8413991125bbe..1a7b0bfc063e5fa63cd8d4bf4b661c09c621a0fb 100644 (file)
@@ -20,9 +20,6 @@
 #include <string.h>
 #include <unistd.h>
 
-struct environ_array *__environ_array_list;
-environ_counter __environ_counter;
-
 char *
 getenv (const char *name)
 {
diff --git a/stdlib/tst-getenv-static.c b/stdlib/tst-getenv-static.c
new file mode 100644 (file)
index 0000000..f5f484c
--- /dev/null
@@ -0,0 +1,38 @@
+/* Static interposition of getenv (bug 32541).
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <stdlib.h>
+#include <support/check.h>
+
+/* Some programs try to interpose getenv for their own use (not
+   glibc's internal use).  Make sure that this is possible without
+   introducing linker failures due to duplicate symbols.  */
+
+char *
+getenv (const char *ignored)
+{
+  return NULL;
+}
+
+static int
+do_test (void)
+{
+  TEST_COMPARE_STRING (getenv ("PATH"), NULL);
+  return 0;
+}
+
+#include <support/test-driver.c>