libc/stdlib/getenv.c: always allocate new environment

in particular, if the old environment is NULL.

Among making it less surprising for userspace to observe NULL environ,
the change also prevents NULL deref in __rebuild_environ() when
terminating the empty as NULL environment with the NULL pointer.

Reported by:	 Leo Bicknell <[email protected]>
PR:	298747
Reviewed by:	emaste, markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D59996
This commit is contained in:
Konstantin Belousov committed 2026-10-02 22:48:42 +03:00
1 parent 782c6ea086
commit dec68aeff8
1 file changed
+1 -1
+1 -1
View File
@@ -268,7 +268,7 @@ __rebuild_environ(int newEnvironSize)
int tmpEnvironSize;
/* Resize environ. */
if (newEnvironSize > environSize) {
if (newEnvironSize > environSize || intEnviron == NULL) {
tmpEnvironSize = newEnvironSize * 2;
tmpEnviron = reallocarray(intEnviron, tmpEnvironSize + 1,
sizeof(*intEnviron));