1
0
mirror of https://git.savannah.gnu.org/git/gnulib.git synced 2026-10-24 00:48:56 +03:00

strnul: Accept 'void *' and 'const void *' arguments in C mode.

Reported by Paul Eggert in
<https://lists.gnu.org/archive/html/bug-gnulib/2026-02/msg00170.html>.

* lib/string.in.h (strnul): Use a conditional expression in _Generic.
* tests/test-strnul.c (main): Add test cases with 'void *' argument.
This commit is contained in:
Bruno Haible
2026-02-23 15:07:30 +01:00
parent 72223de59c
commit 85fbf81245
3 changed files with 25 additions and 3 deletions

View File

@@ -1,3 +1,11 @@
2026-02-23 Bruno Haible <bruno@clisp.org>
strnul: Accept 'void *' and 'const void *' arguments in C mode.
Reported by Paul Eggert in
<https://lists.gnu.org/archive/html/bug-gnulib/2026-02/msg00170.html>.
* 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 <bruno@clisp.org>
fts: Relicense some of its source code under LGPLv2+.

View File

@@ -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))

View File

@@ -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;
}