Some functions return a pointer to an object that cannot be modified without causing undefined behavior. These functions include the standard getenv()
, setlocale()
, localeconv()
, and strerror()
functions. In such cases, the function call results must be treated as being const
qualified.
Subclause 7.22.4.6, paragraph 4, of the C Standard [ISO/IEC 9899:2011] defines getenv()
as follows:
...
Similarly, subclause 7.11.1.1 paragraph 1, paragraph 8 [ISO/IEC 9899:2011], defines setlocale()
as follows:
The pointer to string returned by the
setlocale
function is such that a subsequent call with that string value and its associated category will restore that part of the program's locale. The string pointed to shall not be modified by the program, but may be overwritten by a subsequent call to thesetlocale
function.
And subclause 7.11.1.2, paragraph 8 [ISO/IEC 9899:2011], defines localeconv()
as follows:
...
Finally, subclause 7.24.6.2, paragraph 4 [ISO/IEC 9899:2011], states:
The
strerror
function returns a pointer to the string, the contents of which are locale-specific. The array pointed to shall not be modified by the program, but may be overwritten by a subsequent call to thestrerror
function.
...
CERT C++ Secure Coding Standard | ENV30-CPP. Do not modify the string returned by getenv() |
ISO/IEC TS 17961 | Modifying the string returned by getenv, localeconv, setlocale, and strerror [libmod] |
Bibliography
[IEEE Std 1003.1:2013] | XSH, System Interfaces, getenv XSH, System Interfaces, setlocale XSH, System Interfaces, localeconv | ||
[ISO/IEC 9899:2011] | Subclause 7.11.1.1, "The setlocale Function"Subclause 7.11.1.2, "The localeconv Function"Subclause 7.22.4.6, "The getenv Function"Subclause 7.24.6.2, "The strerror Function" | [Open Group 2004] | getenv setlocale |
...