]> git.ipfire.org Git - thirdparty/glibc.git/commit
Implement C23 const-preserving standard library macros
authorJoseph Myers <josmyers@redhat.com>
Thu, 20 Nov 2025 19:30:27 +0000 (19:30 +0000)
committerJoseph Myers <josmyers@redhat.com>
Thu, 20 Nov 2025 19:31:04 +0000 (19:31 +0000)
commitcd748a63ab1a7ae846175c532a3daab341c62690
tree9a10b61d9a7afed5f6c6fe662e5b31d8d83cdade
parent64b6813338d836ad1b61969a153192fb739f8722
Implement C23 const-preserving standard library macros

C23 makes various standard library functions, that return a pointer
into an input array, into macros that return a pointer to const when
the relevant argument passed to the macro is a pointer to const.  (The
requirement is for macros, with the existing function types applying
when macro expansion is suppressed.  When a null pointer constant is
passed, such as integer 0, that's the same as a pointer to non-const.)

Implement this feature.  This only applies to C, not C++, since such
macros are not an appropriate way of doing this for C++ and all the
affected functions other than bsearch have overloads to implement an
equivalent feature for C++ anyway.  Nothing is done to apply such a
change to any non-C23 functions with the same property of returning a
pointer into an input array.

The feature is also disabled when _LIBC is defined, since there are
various places in glibc that either redefine these identifiers as
macros, or define the functions themselves, and would need changing to
work in the presence of these macro definitions.  A natural question
is whether we should in fact change those places and not disable the
macro definitions for _LIBC.  If so, we'd need a solution for the
places in glibc that define the macro *before* including the relevant
header (in order in effect to disable the header declaration of the
function by renaming that declaration).

One testcase has #undef added to avoid conflicting with this feature
and another has const added; -Wno-discarded-qualifiers is added for
building zic (but could be removed once there's a new upstream tzcode
release that's const-safe with this C23 change and glibc has updated
to code from that new release).  Probably other places in glibc proper
would need const added if we remove the _LIBC conditionals.

Another question would be whether some GCC extension should be added
to support this feature better with macros that only expand each
argument once (as well as reducing duplication of diagnostics for bad
usages such as non-pointer and pointer-to-volatile-qualfied
arguments).

Tested for x86_64.
16 files changed:
NEWS
debug/tst-backtrace.h
manual/search.texi
manual/string.texi
misc/sys/cdefs.h
stdlib/Makefile
stdlib/stdlib.h
stdlib/tst-const.c [new file with mode: 0644]
string/Makefile
string/string.h
string/test-strnlen.c
string/tst-const.c [new file with mode: 0644]
timezone/Makefile
wcsmbs/Makefile
wcsmbs/tst-const.c [new file with mode: 0644]
wcsmbs/wchar.h