N 2720: Sane C library, when wanted

Submitter: Philipp Klaus Krause
Submission Date: 2021-05-09


Fixing small issues in the C library, when programmers indicate they want it fixed

This changes the return types of some functions in the C library, for those who want it.


Some functions in the C library have the wrong return type for historical reasons. Fixing it could affect badly-written programs, so we can't (see what happened to N2526 and N2541). However, we can make the fix conditional on programmers explicitly indicating that the fix is wanted.

Wanting the fix would be indicated by defining a WANT macro.


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.

The return type of the character classification functions in ctype.h and wctype.h is int, despite the information returned being just true vs. false.

The macro could be called __STDC_WANT_LIB_SANE__. Using SANE rather than NEW indicates that this is just about fixing problems in the existing library, not about a bigger libraryy redesign.

Programs might link together parts compiled with with WANT macro with parts without the WANT macro. There is no known implementation that uses a different ABI depending on the return type being char * vs. const char* (see also previous discussion of N2526), so there would be no ABI issue for the 4 functions. For the character classification functions this could be more problematic. One option would be have wording along hte lines of "if the macro __STDC_WANT_LIB_SANE__ is defined, it is unspecified whether any function declared in is a macro or an identifier declared with external linkage. If a macro definition is suppressed in order to access an actual function, or a program defines an external identifier with the name of a function, the behavior is undefined."

Do we want a WANT macro for fixing small issues in the C library?

If yes, what should the name of the macro be?

If yes, should the return type of getenv, localeconv, setlocale, strerror change to const char * when the macro is defined?

If yes, should the return type of the character classification functions change to bool when the macro is defined?