...
The
getenv
function returns a pointer to a string associated with the matched list member. The string pointed to shall not be modified by the program but may be overwritten by a subsequent call to thegetenv
function.
This gives an implementation the latitude, for example, to return a pointer to a statically allocated buffer of static storage duration. Consequently, do not to store this pointer because it may be overwritten by a subsequent call to the getenv()
function or invalidated as a result of changes made to the environment list through calls to the POSIX functions putenv()
or setenv()
, or other implementation-specific means. Storing the pointer for later use can result in a dangling pointer or a pointer to incorrect data. This string should be referenced immediately and discarded; if later use is anticipated, the string should be copied so the copy can be safely referenced as needed.
...
The localeconv()
, setlocale()
, and strerror()
functions have similar caveatsrestrictions. Do not access the objects returned by any of these functions after a subsequent call.
...
Annex K provides the getenv_s()
function for getting a value from the current environment. However, note that according to the standard, getenv_s()
can still have data races with other threads of execution that modify the environment list.
...