The C standard says Standard says getenv() has the following behavior [ISO/IEC 9899:2011]:
...
Consequently, it is best not to store this pointer , as 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 putenv(), setenv(), or other 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.
...
Compliant Solution (Windows)
Windows provides the getenv_s() and _wgetenv_s() functions for getting a value from the current environment [MSDN].
...
Windows also provides the _dupenv_s() and _wdupenv_s() functions for getting a value from the current environment [MSDN]. The _dupenv_s() function searches the list of environment variables for a specified name. If the name is found, a buffer is allocated; the variable's value is copied into the buffer, and the buffer's address and number of elements are returned. By allocating the buffer itself, _dupenv_s() and _wdupenv_s() provide a more convenient alternative to getenv_s() and _wgetenv_s().
...
Tool | Version | Checker | Description |
|---|---|---|---|
Compass/ROSE |
|
|
|
Related Guidelines
...
...
...
| TS 17961 (Draft) | Using an object overwritten by getenv, localeconv, setlocale, and strerror [libuse] |
Bibliography
...
| 9899:2011] | Section 7.22.4, "Communication with the Environment" Section 7.22.4.6, "The getenv Function" |
...
...
getenv_s(), _wgetenv_s() | |
| [Open Group 2004] | Chapter 8, "Environment Variables |
...
| " |
...
strdup | |
| [Viega 2003] | Section 3.6, "Using |
...
| Environment Variables Securely" |
...