From cdc541ad521fa09a0d0ee395d735ac5d3054ce61 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 15 Nov 2025 15:49:23 -0800 Subject: [PATCH] Prefer countof to sizeof / sizeof C2y plans to introduce a new countof operator that will be convenient for GNU tar, so start using it now via Gnulib. * gnulib.modules: Add stdcountof-h. * lib/wordsplit.c, src/buffer.c, src/suffix.c, src/tar.c: Include stdcountof.h, and prefer countof (X) to sizeof X / sizeof *X. --- gnulib.modules | 1 + lib/wordsplit.c | 4 ++-- src/buffer.c | 10 +++++----- src/suffix.c | 5 +++-- src/tar.c | 5 +++-- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/gnulib.modules b/gnulib.modules index ede48729..93c88934 100644 --- a/gnulib.modules +++ b/gnulib.modules @@ -102,6 +102,7 @@ selinux-at setenv stat-time std-gnu23 +stdcountof-h stddef-h stdint-h stpcpy diff --git a/lib/wordsplit.c b/lib/wordsplit.c index 22057209..a777d16a 100644 --- a/lib/wordsplit.c +++ b/lib/wordsplit.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -2590,14 +2591,13 @@ static char const *const wordsplit_errstr[] = { N_("unbalanced parenthesis"), N_("globbing error") }; -enum { wordsplit_nerrs = sizeof wordsplit_errstr / sizeof *wordsplit_errstr }; const char * wordsplit_strerror (struct wordsplit const *ws) { if (ws->ws_errno == WRDSE_USERERR) return ws->ws_usererr; - if (ws->ws_errno < wordsplit_nerrs) + if (ws->ws_errno < countof (wordsplit_errstr)) return wordsplit_errstr[ws->ws_errno]; return N_("unknown error"); } diff --git a/src/buffer.c b/src/buffer.c index d9ffaa7d..3fde3feb 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -33,6 +33,8 @@ #include "common.h" #include +#include + /* Work around GCC bug 117236. */ # if 13 <= __GNUC__ # pragma GCC diagnostic ignored "-Wnull-dereference" @@ -328,7 +330,6 @@ static struct zip_magic const magic[] = { { ct_xz, 6, "\xFD" "7zXZ" }, { ct_zstd, 4, "\x28\xB5\x2F\xFD" }, }; -enum { n_zip_magic = sizeof magic / sizeof *magic }; static struct zip_program zip_program[] = { { ct_compress, COMPRESS_PROGRAM, "-Z" }, @@ -343,14 +344,13 @@ static struct zip_program zip_program[] = { { ct_xz, XZ_PROGRAM, "-J" }, { ct_zstd, ZSTD_PROGRAM, "--zstd" }, }; -enum { n_zip_programs = sizeof zip_program / sizeof *zip_program }; static struct zip_program const * find_zip_program (enum compress_type type, int *pstate) { int i; - for (i = *pstate; i < n_zip_programs; i++) + for (i = *pstate; i < countof (zip_program); i++) { if (zip_program[i].type == type) { @@ -367,7 +367,7 @@ first_decompress_program (int *pstate) { struct zip_program const *zp; - *pstate = n_zip_programs; + *pstate = countof (zip_program); if (use_compress_program_option) return use_compress_program_option; @@ -427,7 +427,7 @@ check_compressed_archive (bool *pshort) /* Probably a valid header */ return ct_tar; - for (p = magic + 2; p < magic + n_zip_magic; p++) + for (p = magic + 2; p < magic + countof (magic); p++) if (memeq (record_start->buffer, p->magic, p->length)) return p->type; diff --git a/src/suffix.c b/src/suffix.c index b1118f15..2fb22da1 100644 --- a/src/suffix.c +++ b/src/suffix.c @@ -19,6 +19,8 @@ #include #include "common.h" +#include + struct compression_suffix { char suffix[sizeof "tbz2"]; /* "tbz2" is tied for longest. */ @@ -72,8 +74,7 @@ find_compression_suffix (char const *name, idx_t *ret_len) suf++; for (struct compression_suffix const *p = compression_suffixes; - p < (compression_suffixes - + sizeof compression_suffixes / sizeof *compression_suffixes); + p < compression_suffixes + countof (compression_suffixes); p++) if (streq (p->suffix, suf)) return p; diff --git a/src/tar.c b/src/tar.c index b7772981..7dcdf68a 100644 --- a/src/tar.c +++ b/src/tar.c @@ -136,6 +136,8 @@ bool delay_directory_restore_option; #include #include +#include + /* Local declarations. */ #ifndef DEFAULT_ARCHIVE_FORMAT @@ -1106,12 +1108,11 @@ decode_signal (const char *name) { "INT", SIGINT }, { "QUIT", SIGQUIT } }; - enum { nsigtab = sizeof sigtab / sizeof *sigtab }; char const *s = name; if (strncmp (s, "SIG", 3) == 0) s += 3; - for (struct sigtab const *p = sigtab; p < sigtab + nsigtab; p++) + for (struct sigtab const *p = sigtab; p < sigtab + countof (sigtab); p++) if (streq (p->name, s)) return p->signo; paxfatal (0, _("Unknown signal name: %s"), name); -- 2.47.3