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.
C99 \[ [ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] defines {{ Wiki Markup getenv
}} as follows:
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. If the specified name cannot be found, a null pointer is returned.
Consequently, if the string returned by getenv()
must be altered, a local copy should be created. Altering the string returned by getenv()
results in undefined behavior. See also undefined behavior 174 of Annex J of C99.
Similarly, C99 \ [[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] defines {{ Wiki Markup setlocale
}} and {{localeconv
}} 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.
...
Altering the string returned by setlocale()
or the structure returned by localeconv()
results in undefined behavior. See also undefined behavior 114 and 115 of Annex J of C99. Furthermore, C99 imposes no requirements on the contents of the string by setlocale()
. Consequently, a program should make no assumptions as to the string's internal contents or structure.unmigrated-wiki-markup
Finally, C99, Section 7.21.6.2 \ [[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] 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.
...
ISO/IEC 9899:1999 Section 7.11.1.1, âThe setlocale
function;â Section 7.11.2.1, âThe localeconv
function;â Section 7.20.4.5, "The getenv
function;" Section 7.21.6.2, "The strerror
function"
Bibliography
...
\[[Open Group 2004|AA. Bibliography#Open Group 04] \] [getenv|http://www.opengroup.org/onlinepubs/000095399/functions/getenv.html], [setlocale|http://www.opengroup.org/onlinepubs/009695399/functions/setlocale.html], [localeconv|http://www.opengroup.org/onlinepubs/009695399/functions/localeconv.html]
...
ENV04-C. Do not call system() if you do not need a command processor 10. Environment (ENV)