Alejandro Colomar wrote:
> I'll do something easier than giving you a large amount of patches.
> Any string literal is rejected, as far as I can see.
> ...
> I've also added a patch to add this reproducer to the gnulib tests

That is easier, indeed.

I'm applying these three patches: to fix the issue and to verify
other kinds of string literals as well.


2025-10-08  Bruno Haible  <[email protected]>

        stdcountof-h tests: Test more kinds of string literals.
        * tests/test-stdcountof-h.c (test_func): Test also wide string literals
        and Unicode string literals.

2025-10-08  Alejandro Colomar  <[email protected]>

        stdcountof-h tests: Test a string literal.
        * tests/test-countof-h.c (test_func): Test a string literal.

2025-10-08  Bruno Haible  <[email protected]>

        stdcountof-h: Add support for string literal arguments in C++ mode.
        Reported by Alejandro Colomar <[email protected]> in
        <https://lists.gnu.org/archive/html/bug-gnulib/2025-10/msg00010.html>.
        * lib/stdcountof.in.h (_gl_array_type_test): Add a partial instantiation
        for string literals.

>From 5124e917b1b2304f5ca2b72e2575a00af8b198ca Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Wed, 8 Oct 2025 14:10:13 +0200
Subject: [PATCH 1/3] stdcountof-h: Add support for string literal arguments in
 C++ mode.

Reported by Alejandro Colomar <[email protected]> in
<https://lists.gnu.org/archive/html/bug-gnulib/2025-10/msg00010.html>.

* lib/stdcountof.in.h (_gl_array_type_test): Add a partial instantiation
for string literals.
---
 ChangeLog           | 8 ++++++++
 lib/stdcountof.in.h | 6 +++++-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index b8f98a18ae..2fa327c5d5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2025-10-08  Bruno Haible  <[email protected]>
+
+	stdcountof-h: Add support for string literal arguments in C++ mode.
+	Reported by Alejandro Colomar <[email protected]> in
+	<https://lists.gnu.org/archive/html/bug-gnulib/2025-10/msg00010.html>.
+	* lib/stdcountof.in.h (_gl_array_type_test): Add a partial instantiation
+	for string literals.
+
 2025-10-03  Bruno Haible  <[email protected]>
 
 	localename-unsafe: Fix handling of yue-Hans locale on macOS.
diff --git a/lib/stdcountof.in.h b/lib/stdcountof.in.h
index 93c7f6bb75..6e602b4a9f 100644
--- a/lib/stdcountof.in.h
+++ b/lib/stdcountof.in.h
@@ -72,11 +72,15 @@ template <typename T>
 /* Bounded arrays.  */
 template <typename T, size_t N>
   struct _gl_array_type_test<T[N]> { static const int is_array = 1; };
+/* String literals.  */
+template <typename T, size_t N>
+  struct _gl_array_type_test<T const (&)[N]> { static const int is_array = 1; };
 #   define _gl_verify_is_array(a) \
      sizeof (_gl_verify_type<_gl_array_type_test<decltype(a)>::is_array>)
 #  else
   /* Use template argument deduction.
-     Use sizeof to get a constant expression from an unknown type.  */
+     Use sizeof to get a constant expression from an unknown type.
+     Note: This approach does not work for countof (((int[]) { a, b, c })).  */
 /* Default case.  */
 template <typename T>
   struct _gl_array_type_test { double large; };
-- 
2.51.0

>From 13cb0c83498d2953bad456cea927f9cea20961a9 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Wed, 8 Oct 2025 11:40:58 +0200
Subject: [PATCH 2/3] stdcountof-h tests: Test a string literal.

* tests/test-stdcountof-h.c (test_func): Test a string literal.

Copyright-paperwork-exempt: Yes
---
 ChangeLog                 | 5 +++++
 tests/test-stdcountof-h.c | 3 +++
 2 files changed, 8 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 2fa327c5d5..8714bd99dc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2025-10-08  Alejandro Colomar  <[email protected]>
+
+	stdcountof-h tests: Test a string literal.
+	* tests/test-countof-h.c (test_func): Test a string literal.
+
 2025-10-08  Bruno Haible  <[email protected]>
 
 	stdcountof-h: Add support for string literal arguments in C++ mode.
diff --git a/tests/test-stdcountof-h.c b/tests/test-stdcountof-h.c
index b64bd9d58b..4965cf7bd7 100644
--- a/tests/test-stdcountof-h.c
+++ b/tests/test-stdcountof-h.c
@@ -46,11 +46,13 @@ test_func (int parameter[3])
   (void) _gl_verify_is_array (unbounded);
   (void) _gl_verify_is_array (bounded);
   (void) _gl_verify_is_array (multidimensional);
+  (void) _gl_verify_is_array ("string");
 #endif
 
   ASSERT (countof (bounded) == 10);
   ASSERT (countof (multidimensional) == 10);
   ASSERT (countof (local_bounded) == 20);
+  ASSERT (countof ("string") == 6 + 1);
 
 #if 0 /* These produce compilation errors.  */
 # ifdef _gl_verify_is_array
@@ -72,6 +74,7 @@ test_func (int parameter[3])
   ASSERT (_Generic (countof (bounded),          size_t: 1, default: 0));
   ASSERT (_Generic (countof (multidimensional), size_t: 1, default: 0));
   ASSERT (_Generic (countof (local_bounded),    size_t: 1, default: 0));
+  ASSERT (_Generic (countof ("string"),         size_t: 1, default: 0));
 #endif
 }
 
-- 
2.51.0

>From 7b46a2818376613dc33cf89820c359379d3758cf Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Wed, 8 Oct 2025 14:12:51 +0200
Subject: [PATCH 3/3] stdcountof-h tests: Test more kinds of string literals.

* tests/test-stdcountof-h.c (test_func): Test also wide string literals
and Unicode string literals.
---
 ChangeLog                 |  6 ++++++
 tests/test-stdcountof-h.c | 18 ++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 8714bd99dc..ec2d9651a5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2025-10-08  Bruno Haible  <[email protected]>
+
+	stdcountof-h tests: Test more kinds of string literals.
+	* tests/test-stdcountof-h.c (test_func): Test also wide string literals
+	and Unicode string literals.
+
 2025-10-08  Alejandro Colomar  <[email protected]>
 
 	stdcountof-h tests: Test a string literal.
diff --git a/tests/test-stdcountof-h.c b/tests/test-stdcountof-h.c
index 4965cf7bd7..77cfbc4dec 100644
--- a/tests/test-stdcountof-h.c
+++ b/tests/test-stdcountof-h.c
@@ -47,12 +47,24 @@ test_func (int parameter[3])
   (void) _gl_verify_is_array (bounded);
   (void) _gl_verify_is_array (multidimensional);
   (void) _gl_verify_is_array ("string");
+  (void) _gl_verify_is_array (L"wide string");
+# if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L
+  (void) _gl_verify_is_array (u8"UTF-8 string");
+  (void) _gl_verify_is_array (u"UTF-16 string");
+  (void) _gl_verify_is_array (U"UTF-32 string");
+# endif
 #endif
 
   ASSERT (countof (bounded) == 10);
   ASSERT (countof (multidimensional) == 10);
   ASSERT (countof (local_bounded) == 20);
   ASSERT (countof ("string") == 6 + 1);
+  ASSERT (countof (L"wide string") == 11 + 1);
+#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L
+  ASSERT (countof (u8"UTF-8 string") == 12 + 1);
+  ASSERT (countof (u"UTF-16 string") == 13 + 1);
+  ASSERT (countof (U"UTF-32 string") == 13 + 1);
+#endif
 
 #if 0 /* These produce compilation errors.  */
 # ifdef _gl_verify_is_array
@@ -75,6 +87,12 @@ test_func (int parameter[3])
   ASSERT (_Generic (countof (multidimensional), size_t: 1, default: 0));
   ASSERT (_Generic (countof (local_bounded),    size_t: 1, default: 0));
   ASSERT (_Generic (countof ("string"),         size_t: 1, default: 0));
+  ASSERT (_Generic (countof (L"wide string"),   size_t: 1, default: 0));
+# if __STDC_VERSION__ >= 201112L
+  ASSERT (_Generic (countof (u8"UTF-8 string"), size_t: 1, default: 0));
+  ASSERT (_Generic (countof (u"UTF-16 string"), size_t: 1, default: 0));
+  ASSERT (_Generic (countof (U"UTF-32 string"), size_t: 1, default: 0));
+# endif
 #endif
 }
 
-- 
2.51.0

Reply via email to