Skip to content

Fix compiler warning in _ctypes_test on strchr()#149791

Open
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:ctypes_test
Open

Fix compiler warning in _ctypes_test on strchr()#149791
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:ctypes_test

Conversation

@vstinner
Copy link
Copy Markdown
Member

Change my_strchr() return type to "const char*" (add "const").

Fix the compiler warning:

Modules/_ctypes/_ctypes_test.c: In function 'my_strchr': Modules/_ctypes/_ctypes_test.c:451:12: warning: return discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
  451 |     return strchr(s, c);
      |            ^~~~~~

When using C23, strchr(text, ch) return type is "const char*" if text type is "const char*".

Change my_strchr() return type to "const char*" (add "const").

Fix the compiler warning:

Modules/_ctypes/_ctypes_test.c: In function 'my_strchr':
Modules/_ctypes/_ctypes_test.c:451:12: warning: return discards
'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
  451 |     return strchr(s, c);
      |            ^~~~~~

When using C23, strchr(text, ch) return type is "const char*" if text
type is "const char*".
@vstinner
Copy link
Copy Markdown
Member Author

See for example the glibc commit which introduced C23 support in string/string.h.

The code is now:

extern char *strchr (const char *__s, int __c)
     __THROW __attribute_pure__ __nonnull ((1));
# if __GLIBC_USE (ISOC23) && defined __glibc_const_generic && !defined _LIBC
#  define strchr(S, C)                                         \
  __glibc_const_generic (S, const char *, strchr (S, C))
# endif

@vstinner vstinner added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes skip issue skip news

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant