Re: git: f1d98862044f - main - libc: getopt{,_long}: Const correctness for C23
- In reply to: Lexi Winter : "git: f1d98862044f - main - libc: getopt{,_long}: Const correctness for C23"
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 03 Aug 2026 15:06:48 UTC
On 8/3/26 07:30, Lexi Winter wrote:
> The branch main has been updated by ivy:
>
> URL: https://cgit.FreeBSD.org/src/commit/?id=f1d98862044f7748c6f930e9d4339abc166a5b16
>
> commit f1d98862044f7748c6f930e9d4339abc166a5b16
> Author: Lexi Winter <ivy@FreeBSD.org>
> AuthorDate: 2026-08-03 14:02:51 +0000
> Commit: Lexi Winter <ivy@FreeBSD.org>
> CommitDate: 2026-08-03 14:02:51 +0000
>
> libc: getopt{,_long}: Const correctness for C23
>
> On some platforms, e.g. Linux Clang 22.1.8 / glibc 2.43, strchr()
> now implements the C23 behaviour where passing a const pointer to
> strchr() also returns a const pointer.
A note on the terminology in this and the related commits:
"const char *NAME;" is a (non-const) pointer to a const char, not a
const pointer to a (non-const) char. In short: pointer to const is now
being put to use, not const pointer.
> This breaks getopt during
> the bootstrap build, since it assumes the return value is always
> a mutable pointer.
>
> Since the pointed-to value is never modified, fix this by making
> the pointer const.
>
> MFC after: 1 week
> Reviewed by: emaste
> Sponsored by: The FreeBSD Foundation
> Differential Revision: https://reviews.freebsd.org/D58488
> ---
> lib/libc/stdlib/getopt.c | 2 +-
> lib/libc/stdlib/getopt_long.c | 2 +-
> 2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/lib/libc/stdlib/getopt.c b/lib/libc/stdlib/getopt.c
> index 2b5e3fa69032..e1edc5a3ee83 100644
> --- a/lib/libc/stdlib/getopt.c
> +++ b/lib/libc/stdlib/getopt.c
> @@ -57,7 +57,7 @@ int
> getopt(int nargc, char * const nargv[], const char *ostr)
> {
> static char *place = EMSG; /* option letter processing */
> - char *oli; /* option letter list index */
> + const char *oli; /* option letter list index */
>
> if (optreset || *place == 0) { /* update scanning pointer */
> optreset = 0;
> diff --git a/lib/libc/stdlib/getopt_long.c b/lib/libc/stdlib/getopt_long.c
> index 5cf6a55649bd..99be520fe332 100644
> --- a/lib/libc/stdlib/getopt_long.c
> +++ b/lib/libc/stdlib/getopt_long.c
> @@ -349,7 +349,7 @@ static int
> getopt_internal(int nargc, char * const *nargv, const char *options,
> const struct option *long_options, int *idx, int flags)
> {
> - char *oli; /* option letter list index */
> + const char *oli; /* option letter list index */
> int optchar, short_too;
> static int posixly_correct = -1;
>
>
>
--
===
Mark Millard
marklmi at yahoo.com