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". I
suggest to make these functions return pointers-to-const, e.g.
change
char *strerror(int errnum);
to
const char *strerror(int errnum);
A draft proposal is at http://www.colecovision.eu/stuff/proposal-const-shall-not-be-modified-stdlib.html
Do you see any problem with this change?
I was wondering if it make sense to also change the char * members of
struct lconf to const char *, but I fear that change might break more stuff.
Philipp Klaus Krause <p...@spth.de> writes:...
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". I
suggest to make these functions return pointers-to-const, e.g.
It would likely break existing code, for the same reason that making
string literals const would break existing code.
Any program that has something like:
char *username = getenv("USER");
would have to be modified. That would include any pre-ANSI code that
calls getenv.
Would limiting the const function prototypes to C2x code (gcc -std=c2x) address this concern about backwards compatibility? Old code that wants the non-const function prototypes can ask for an older C version (e.g. gcc -std=c18).
On Wednesday, May 6, 2020 at 1:18:54 PM UTC-7, Keith Thompson wrote:
Philipp Klaus Krause <p...@spth.de> writes:...
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". I
suggest to make these functions return pointers-to-const, e.g.
It would likely break existing code, for the same reason that making
string literals const would break existing code.
Any program that has something like:
char *username = getenv("USER");
would have to be modified. That would include any pre-ANSI code that
calls getenv.
Would limiting the const function prototypes to C2x code (gcc -std=c2x) address this concern about backwards compatibility? Old code that wants the non-const function prototypes can ask for an older C version (e.g. gcc -std=c18).
#if __STDC_VERSION__ > 201710L
const char *getenv(const char *name);
#else
char *getenv(const char *name);
#endif
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 339 |
Nodes: | 16 (1 / 15) |
Uptime: | 86:34:11 |
Calls: | 7,480 |
Files: | 12,703 |
Messages: | 5,634,097 |