]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
strnul: Accept 'void *' and 'const void *' arguments in C mode.
authorBruno Haible <bruno@clisp.org>
Mon, 23 Feb 2026 14:07:30 +0000 (15:07 +0100)
committerBruno Haible <bruno@clisp.org>
Mon, 23 Feb 2026 14:07:30 +0000 (15:07 +0100)
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.

ChangeLog
lib/string.in.h
tests/test-strnul.c

index e50f5bbafd6ad81f6aab4bc22098d06500d8560f..e0c557dd6165a9f032378b6f5d8ebf3f470ff974 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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+.
index 599203c44db9e18c387a3b17847c62d6d18b88a8..33422a06e766a9715ba63509ba4f4d4e47ab9e81 100644 (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))
index b9b4970ad64d793d1a3acc304cadfed123da090c..7c068d8f1ec2f70ec5c5cbac1d5a96cc560dfd7f 100644 (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;
 }