Skip to contentSkip to conversation

New: strstr/c23/constness

Bug 221533 [email protected] 1 participant 4 messages May 16 – May 17 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

https://bugzilla.kernel.org/show_bug.cgi?id=221533

            Bug ID: 221533
           Summary: strstr/c23/constness
           Product: Documentation
           Version: unspecified
          Hardware: All
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P3
         Component: man-pages
          Assignee: [email protected]
          Reporter: [email protected]
        Regression: No

Hi,
In C23 strstr can return a const char* if the first parameter is a const
char*; this is also true of a bunch of the other routines.

I'm not really sure how this should be documented, I guess two entries for
each function with some note saying it's only C23 and later?

See:
https://sourceware.org/cgit/glibc/commit/string/string.h?id=cd748a63ab1a7ae846175c532a3daab341c62690

for the glibc change that implemented it.

It can break some existing code, e.g. something like:

int foo(const char *a)
{
char *bar = strstr(a, "frog");
}

gives:
t.c:9:16: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

    9 |    char *bar = strstr(a, "frog");
      |                ^~~~~~
Signature · 4 lines
You may reply to this email to add a comment.

You are receiving this mail because:
You are watching the assignee of the bug.
#1 lore raw

[Bug 221533] strstr/c23/constness

https://bugzilla.kernel.org/show_bug.cgi?id=221533

--- Comment #1 from Alejandro Colomar ([email protected]) --- Hi,

Quoted 6 lines · On 2026-05-16T16:47:42+0000, [email protected] wrote:

Hi,
In C23 strstr can return a const char* if the first parameter is a const
char*; this is also true of a bunch of the other routines.

I'm not really sure how this should be documented, I guess two entries for
each function with some note saying it's only C23 and later?

I've been thinking about this. At the moment, I've documented one function that has this property: strnul(3) (which is a gnulib extension, not part of C23). There, I've used C++-style, documenting it with two prototypes, as if it were a function overload. I think we should do the same for the C23 const-preserving macros.

        SYNOPSIS
             #include <string.h>

             char *strnul(char *s);
             const char *strnul(const char *s);

I'll work on some patches for the C23 APIs.

Quoted 7 lines

See:

https://sourceware.org/cgit/glibc/commit/string/string.h?id=cd748a63ab1a7ae846175c532a3daab341c62690

for the glibc change that implemented it.

It can break some existing code, e.g. something like:

Indeed, it broke mutt(1), for example. That allowed finding a few bugs in mutt(1), which were modifying read-only strings. Most of the cases were false positives, though, which could be fixed easily by adding const.

Quoted 10 lines

int foo(const char *a)

{
   char *bar = strstr(a, "frog");
}

gives:
t.c:9:16: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

    9 |    char *bar = strstr(a, "frog");
      |                ^~~~~~

Yup.

Have a lovely day!
Alex

Signature · 4 lines
You may reply to this email to add a comment.

You are receiving this mail because:
You are watching the assignee of the bug.
#2 lore raw

[Bug 221533] strstr/c23/constness

In reply to [email protected]

https://bugzilla.kernel.org/show_bug.cgi?id=221533

--- Comment #2 from Dr. David Alan Gilbert ([email protected]) --- Yep, makes sense; I tripped over it yesterday in one of qemu's tests (someone has already sent a test).

Thanks!

Dave

Signature · 4 lines
You may reply to this email to add a comment.

You are receiving this mail because:
You are watching the assignee of the bug.
#3 lore raw

[Bug 221533] strstr/c23/constness

In reply to [email protected]

https://bugzilla.kernel.org/show_bug.cgi?id=221533

Artem S. Tashkinov ([email protected]) changed:

What |Removed |Added
----------------------------------------------------------------------------

             Status|NEW                         |RESOLVED
         Resolution|---                         |ANSWERED
Signature · 4 lines
You may reply to this email to add a comment.

You are receiving this mail because:
You are watching the assignee of the bug.