Re:Getaddrinfo() on glibc calls getenv(), oh boy
by thayne on Monday October 16, 2023 @11:25PM
(#37910195)
Having a lock would still be better than the current situation. Especially if the lock was exposed so that programs that did mess with environ directly, or the pointer returned by getenv could hold the lock while doing so.
Re:Getaddrinfo() on glibc calls getenv(), oh boy
by lazide on Tuesday October 17, 2023 @12:38AM
(#37910692)
Deadlock disaster that one.
Re:Getaddrinfo() on glibc calls getenv(), oh boy
by cryptonector on Tuesday October 17, 2023 @02:20AM
(#37911279)
`getenv()` never returns pointers that can be invalidated. If you `putenv()` something, you commit to never freeing it or overwriting it.
Re:Getaddrinfo() on glibc calls getenv(), oh boy
by hddqsb on Tuesday October 17, 2023 @05:14AM
(#37912376)
Good point about `putenv()`; however there is also `setenv()`, which does make a copy, so you are wrong about `getenv()` in general.
POSIX explicitly states "The string [returned by getenv] may be overwritten by a subsequent call to getenv(), setenv(), unsetenv(), or putenv()" (https://pubs.opengroup.org/onlinepubs/9699919799.2008edition...).
Re:Getaddrinfo() on glibc calls getenv(), oh boy
by cryptonector on Tuesday October 17, 2023 @11:31AM
(#37916719)
"May", but really, it's best for `setenv()` to leak.
NetBSD has getenv_r, which copies into a buffer, but few applications use getenv_r, and certainly not all of them. And it doesn't resolve environ.
Solaris never free's env strings or environ arrays, only creating new copies and atomically swapping them. It uses a special allocator for those objects which doubles the backing buffer each time it deep copies the environ array, then argues this strategy is technically asymptotically memory bounded.
EDIT: Glancing at the code I think glibc is similar to Solaris in that it never free's env strings, but it has a heuristic to conditionally free environ arrays which means directly using environ isn't thread-safe.