top | item 42797226

(no title)

dgrunwald | 1 year ago

The problem with `setenv` is that people expect one process to have one set of environment variables, which is shared across multiple languages running in that process. This implies every language must let its environment variables be managed by a central language-independent library -- and on POSIX systems, that's libc. So if libc refuses to provide thread-safety, that impacts not just C, but all possible languages (except for those that cannot call into C-libraries; as those don't need to bother synchronizing the environment with libc).

discuss

order

PaulDavisThe1st|1 year ago

It's not just that "libc refuses to provide thread-safety" ... the POSIX standard specifies that these functions are non-reentrant.

saagarjha|1 year ago

A conformant implementation can make a non-reentrant function actually safe under the hood for people that call into it erroneously. Unfortunately, there is no way to do this for getenv/setenv, because of the API they expose (specifically, when environ is accessed directly).