n2526: use const for data from the library that shall not be modified

Submitter:Philipp Klaus Krause
Submission Date:2020-05-11

Summary:

use const for some data from the library that shall not be modified.

There are 4 functions (getenv, localeconv, setlocale, strerror) in the standard library that return a pointer and state that the return value points to something that "shall not be modified by the program". The correct way to state this would be to make the return value pointer-to-const. This would communicate the intent more clearly even to users, and make it easier for implementations to diagnose bugs.

This change could break conforming programs, e.g. those that assign the return value of strerror to char *, but do not modify the string. However, such programs are apparently uncommon: A quick search on GitHub for strerror among the first few pages of results only found uses that are consistent with a return type of const char *: Apparenlty most users either directly pass the return value to some function that expects a const char *, or assign it to a const char *.

Do we want to change the return type of these 4 functions to pointers to const?

Proposed changes: §7.11.1.1: "char *setlocale(int category, const char *locale);" to "const char *setlocale(int category, const char *locale);". §7.11.2.1: Change "struct lconv *localeconv(void);" to "const struct lconv *localeconv(void);". §7.22.4.6: Change "char *getenv(const char *name);" to "const char *getenv(const char *name);". §7.24.6.2: Change "char *strerror(int errnum);" to "const char *strerror(int errnum);".