From: Bruno Haible Date: Mon, 23 Feb 2026 14:07:30 +0000 (+0100) Subject: strnul: Accept 'void *' and 'const void *' arguments in C mode. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=85fbf81245c8f7e8dedaa4794bfa209f885d41ba;p=thirdparty%2Fgnulib.git strnul: Accept 'void *' and 'const void *' arguments in C mode. Reported by Paul Eggert in . * lib/string.in.h (strnul): Use a conditional expression in _Generic. * tests/test-strnul.c (main): Add test cases with 'void *' argument. --- diff --git a/ChangeLog b/ChangeLog index e50f5bbafd..e0c557dd61 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2026-02-23 Bruno Haible + + strnul: Accept 'void *' and 'const void *' arguments in C mode. + Reported by Paul Eggert in + . + * lib/string.in.h (strnul): Use a conditional expression in _Generic. + * tests/test-strnul.c (main): Add test cases with 'void *' argument. + 2026-02-23 Bruno Haible fts: Relicense some of its source code under LGPLv2+. diff --git a/lib/string.in.h b/lib/string.in.h index 599203c44d..33422a06e7 100644 --- a/lib/string.in.h +++ b/lib/string.in.h @@ -1271,10 +1271,16 @@ template <> inline char *strnul< char *> ( char *s) || (defined __SUNPRO_C && __SUNPRO_C >= 0x5150) \ || (__STDC_VERSION__ >= 201112L && !defined __GNUC__) /* The compiler supports _Generic from ISO C11. */ +/* Since in C (but not in C++!), any function that accepts a '[const] char *' + also accepts a '[const] void *' as argument, we make sure that the function- + like macro does the same, by mapping its type first: + char *, void * -> void * + const char *, const void * -> const void * + This mapping is done through the conditional expression. */ # define strnul(s) \ - _Generic (s, \ - char * : (char *) gl_strnul (s), \ - const char * : gl_strnul (s)) + _Generic (1 ? (s) : (void *) 99, \ + void * : (char *) gl_strnul (s), \ + const void * : gl_strnul (s)) # else # define strnul(s) \ ((char *) gl_strnul (s)) diff --git a/tests/test-strnul.c b/tests/test-strnul.c index b9b4970ad6..7c068d8f1e 100644 --- a/tests/test-strnul.c +++ b/tests/test-strnul.c @@ -33,5 +33,13 @@ main () ASSERT (ro_nul - ro == 3); ASSERT (rw_nul - rw == 3); +#if !defined __cplusplus + const char *rov_nul = strnul ((const void *) ro); + char *rwv_nul = strnul ((void *) rw); + + ASSERT (rov_nul - ro == 3); + ASSERT (rwv_nul - rw == 3); +#endif + return test_exit_status; }