Two more uses of countof, that we missed in March 2026.

2026-05-11  Bruno Haible  <[email protected]>

        stat-time: Use countof.
        * lib/stat-time.h: Include <stdcountof.h>.
        (stat_time_normalize): Use countof.
        * modules/stat-time (Depends-on): Add stdcountof-h.
        * modules/relocatable-prog-wrapper (Depends-on): Likewise.

        setlocale: Use countof.
        * lib/setlocale.c: Include <stdcountof.h>.
        (setlocale_unixlike, get_main_locale_with_same_language,
        get_main_locale_with_same_territory, setlocale_improved): Use countof.
        * modules/setlocale (Depends-on): Add stdcountof-h.

>From 538a58fef870d065d63d08c04f754243ef55c7ea Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Tue, 12 May 2026 00:54:57 +0200
Subject: [PATCH 1/2] setlocale: Use countof.

* lib/setlocale.c: Include <stdcountof.h>.
(setlocale_unixlike, get_main_locale_with_same_language,
get_main_locale_with_same_territory, setlocale_improved): Use countof.
* modules/setlocale (Depends-on): Add stdcountof-h.
---
 ChangeLog         |  8 ++++++++
 lib/setlocale.c   | 20 +++++++-------------
 modules/setlocale |  1 +
 3 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5e31f790bf..8bf9697303 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2026-05-11  Bruno Haible  <[email protected]>
+
+	setlocale: Use countof.
+	* lib/setlocale.c: Include <stdcountof.h>.
+	(setlocale_unixlike, get_main_locale_with_same_language,
+	get_main_locale_with_same_territory, setlocale_improved): Use countof.
+	* modules/setlocale (Depends-on): Add stdcountof-h.
+
 2026-05-11  Bruno Haible  <[email protected]>
 
 	gettext-h: Ensure no warnings with --disable-nls and -Wformat=2.
diff --git a/lib/setlocale.c b/lib/setlocale.c
index 022105050f..d40e1e2efd 100644
--- a/lib/setlocale.c
+++ b/lib/setlocale.c
@@ -29,6 +29,7 @@
 /* Specification.  */
 #include <locale.h>
 
+#include <stdcountof.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -735,10 +736,7 @@ setlocale_unixlike (int category, const char *locale)
       {
         range_t range;
 
-        search (language_table,
-                sizeof (language_table) / sizeof (language_table[0]),
-                llCC_buf,
-                &range);
+        search (language_table, countof (language_table), llCC_buf, &range);
 
         for (size_t i = range.lo; i < range.hi; i++)
           {
@@ -782,17 +780,13 @@ setlocale_unixlike (int category, const char *locale)
                  and CC_buf in country_table.  */
               range_t language_range;
 
-              search (language_table,
-                      sizeof (language_table) / sizeof (language_table[0]),
-                      ll_buf,
+              search (language_table, countof (language_table), ll_buf,
                       &language_range);
               if (language_range.lo < language_range.hi)
                 {
                   range_t country_range;
 
-                  search (country_table,
-                          sizeof (country_table) / sizeof (country_table[0]),
-                          CC_buf,
+                  search (country_table, countof (country_table), CC_buf,
                           &country_range);
                   if (country_range.lo < country_range.hi)
                     for (size_t i = language_range.lo; i < language_range.hi; i++)
@@ -1162,7 +1156,7 @@ get_main_locale_with_same_language (const char *locale)
 {
 #   define table locales_with_principal_territory
   /* The table is sorted.  Perform a binary search.  */
-  size_t hi = sizeof (table) / sizeof (table[0]);
+  size_t hi = countof (table);
   size_t lo = 0;
   while (lo < hi)
     {
@@ -1391,7 +1385,7 @@ get_main_locale_with_same_territory (const char *locale)
     {
 #   define table locales_with_principal_language
       /* The table is sorted.  Perform a binary search.  */
-      size_t hi = sizeof (table) / sizeof (table[0]);
+      size_t hi = countof (table);
       size_t lo = 0;
       while (lo < hi)
         {
@@ -1483,7 +1477,7 @@ setlocale_improved (int category, const char *locale)
             goto fail;
 #  endif
 
-          for (; i < sizeof (categories) / sizeof (categories[0]); i++)
+          for (; i < countof (categories); i++)
             {
               int cat = categories[i];
               const char *name =
diff --git a/modules/setlocale b/modules/setlocale
index 75c2297387..aff365ac95 100644
--- a/modules/setlocale
+++ b/modules/setlocale
@@ -7,6 +7,7 @@ m4/setlocale.m4
 
 Depends-on:
 locale-h
+stdcountof-h
 streq
 setlocale-fixes    [test $NEED_SETLOCALE_IMPROVED = 1]
 localename         [test $NEED_SETLOCALE_IMPROVED = 1]
-- 
2.54.0

>From 394e0712330d312dc4e1f1b7d6fb15f7d12bfa8b Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Tue, 12 May 2026 00:57:55 +0200
Subject: [PATCH 2/2] stat-time: Use countof.

* lib/stat-time.h: Include <stdcountof.h>.
(stat_time_normalize): Use countof.
* modules/stat-time (Depends-on): Add stdcountof-h.
* modules/relocatable-prog-wrapper (Depends-on): Likewise.
---
 ChangeLog                        | 6 ++++++
 lib/stat-time.h                  | 3 ++-
 modules/relocatable-prog-wrapper | 1 +
 modules/stat-time                | 1 +
 4 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 8bf9697303..d5e8e6fb74 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2026-05-11  Bruno Haible  <[email protected]>
 
+	stat-time: Use countof.
+	* lib/stat-time.h: Include <stdcountof.h>.
+	(stat_time_normalize): Use countof.
+	* modules/stat-time (Depends-on): Add stdcountof-h.
+	* modules/relocatable-prog-wrapper (Depends-on): Likewise.
+
 	setlocale: Use countof.
 	* lib/setlocale.c: Include <stdcountof.h>.
 	(setlocale_unixlike, get_main_locale_with_same_language,
diff --git a/lib/stat-time.h b/lib/stat-time.h
index 4536431664..461a0c88b9 100644
--- a/lib/stat-time.h
+++ b/lib/stat-time.h
@@ -28,6 +28,7 @@
 
 #include <errno.h>
 #include <stdckdint.h>
+#include <stdcountof.h>
 #include <stddef.h>
 #include <sys/stat.h>
 #include <time.h>
@@ -232,7 +233,7 @@ stat_time_normalize (int result, _GL_UNUSED struct stat *st)
       short int const ts_off[] = { STAT_TIMESPEC_OFFSETOF (st_atim),
                                    STAT_TIMESPEC_OFFSETOF (st_mtim),
                                    STAT_TIMESPEC_OFFSETOF (st_ctim) };
-      for (int i = 0; i < sizeof ts_off / sizeof *ts_off; i++)
+      for (int i = 0; i < countof (ts_off); i++)
         {
           struct timespec *ts = (struct timespec *) ((char *) st + ts_off[i]);
           long int q = ts->tv_nsec / timespec_hz;
diff --git a/modules/relocatable-prog-wrapper b/modules/relocatable-prog-wrapper
index e37da6cc18..346767d307 100644
--- a/modules/relocatable-prog-wrapper
+++ b/modules/relocatable-prog-wrapper
@@ -73,6 +73,7 @@ ssize_t
 bool
 stddef-h
 stdckdint-h
+stdcountof-h
 stdint-h
 stdlib-h
 string-h
diff --git a/modules/stat-time b/modules/stat-time
index 424f33d483..83722cd51c 100644
--- a/modules/stat-time
+++ b/modules/stat-time
@@ -12,6 +12,7 @@ errno-h
 extensions
 extern-inline
 stdckdint-h
+stdcountof-h
 
 configure.ac:
 gl_STAT_TIME
-- 
2.54.0

Reply via email to