Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

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.

Wiki MarkupC99 \[ [ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] defines {{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 the getenv 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.

Wiki MarkupSimilarly, C99 \ [[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] defines {{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 the setlocale 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 the strerror 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)