From: Alejandro Colomar Date: Wed, 21 May 2025 11:33:37 +0000 (+0200) Subject: lib/, src/, tests/: Use the standard countof() instead of our NITEMS() X-Git-Tag: 4.18.0-rc1~32 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ba3456c9fd7decc16c6aaacd4c6651cc58438149;p=thirdparty%2Fshadow.git lib/, src/, tests/: Use the standard countof() instead of our NITEMS() countof() is the name blessed by the C Committee for ISO C2y. Use it if available, and define it if not. countof() will be provided by GCC 16 and Clang 21. This is mostly a scripted change: $ grep -rl NITEMS | xargs sed -i s/NITEMS/countof/; Apart from the scripted changes, I've adjusted white-space alignment, and of course the definition at "lib/sizeof.h". Link: Link: Signed-off-by: Alejandro Colomar --- diff --git a/lib/adds.h b/lib/adds.h index 5e4fa2749..b4cd8c258 100644 --- a/lib/adds.h +++ b/lib/adds.h @@ -20,7 +20,7 @@ ({ \ long addend_[] = {a, b, __VA_ARGS__}; \ \ - addslN(NITEMS(addend_), addend_); \ + addslN(countof(addend_), addend_); \ }) diff --git a/lib/audit_help.c b/lib/audit_help.c index 8b2402552..710069b8a 100644 --- a/lib/audit_help.c +++ b/lib/audit_help.c @@ -98,7 +98,7 @@ audit_logger_with_group(int type, const char *op, const char *name, { int len; char enc_group[GROUP_NAME_MAX_LENGTH * 2 + 1]; - char buf[NITEMS(enc_group) + 100]; + char buf[countof(enc_group) + 100]; if (audit_fd < 0) return; diff --git a/lib/fs/readlink/readlinknul.h b/lib/fs/readlink/readlinknul.h index ed2f34d13..0791df57d 100644 --- a/lib/fs/readlink/readlinknul.h +++ b/lib/fs/readlink/readlinknul.h @@ -17,7 +17,7 @@ #include "sizeof.h" -#define READLINKNUL(link, buf) readlinknul(link, buf, NITEMS(buf)) +#define READLINKNUL(link, buf) readlinknul(link, buf, countof(buf)) ATTR_STRING(1) diff --git a/lib/must_be.h b/lib/must_be.h index 6ae6c7998..2c866e7ba 100644 --- a/lib/must_be.h +++ b/lib/must_be.h @@ -40,10 +40,10 @@ * EXAMPLES * #define must_be_array(a) must_be(is_array(a)) * - * #define NITEMS(a) (sizeof(a) / sizeof(*(a)) + must_be_array(a)) + * #define countof(a) (sizeof(a) / sizeof(*(a)) + must_be_array(a)) * * int foo[42]; - * int bar[NITEMS(foo)]; + * int bar[countof(foo)]; */ diff --git a/lib/sizeof.h b/lib/sizeof.h index 6051b7777..fc1a2dd3e 100644 --- a/lib/sizeof.h +++ b/lib/sizeof.h @@ -1,7 +1,5 @@ -/* - * SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar - * SPDX-License-Identifier: BSD-3-Clause - */ +// SPDX-FileCopyrightText: 2022-2025, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause #ifndef SHADOW_INCLUDE_LIBMISC_SIZEOF_H_ @@ -11,6 +9,9 @@ #include #include +#if __has_include() +# include +#endif #include #include "must_be.h" @@ -20,8 +21,12 @@ #define memberof(T, member) ((T){}.member) #define WIDTHOF(x) (sizeof(x) * CHAR_BIT) #define SIZEOF_ARRAY(a) (sizeof(a) + must_be_array(a)) -#define NITEMS(a) (SIZEOF_ARRAY((a)) / sizeof((a)[0])) -#define STRLEN(s) (NITEMS("" s "") - 1) + +#if !defined(countof) +# define countof(a) (SIZEOF_ARRAY((a)) / sizeof((a)[0])) +#endif + +#define STRLEN(s) (countof("" s "") - 1) #endif // include guard diff --git a/lib/string/sprintf/snprintf.h b/lib/string/sprintf/snprintf.h index d181c661a..4395c71f4 100644 --- a/lib/string/sprintf/snprintf.h +++ b/lib/string/sprintf/snprintf.h @@ -18,7 +18,7 @@ #define SNPRINTF(s, fmt, ...) \ ( \ - snprintf_(s, NITEMS(s), fmt __VA_OPT__(,) __VA_ARGS__) \ + snprintf_(s, countof(s), fmt __VA_OPT__(,) __VA_ARGS__) \ ) diff --git a/lib/string/sprintf/stpeprintf.h b/lib/string/sprintf/stpeprintf.h index ccd844bc2..c34e4efa8 100644 --- a/lib/string/sprintf/stpeprintf.h +++ b/lib/string/sprintf/stpeprintf.h @@ -40,7 +40,7 @@ inline char *vstpeprintf(char *dst, char *end, const char *restrict fmt, * * end Pointer to one after the last element of the buffer * pointed to by `dst`. Usually, it should be calculated - * as `dst + NITEMS(dst)`. + * as `dst + countof(dst)`. * * fmt Format string * diff --git a/lib/string/strcpy/stpecpy.h b/lib/string/strcpy/stpecpy.h index e9debe4ec..cc7dc0a2d 100644 --- a/lib/string/strcpy/stpecpy.h +++ b/lib/string/strcpy/stpecpy.h @@ -32,7 +32,7 @@ inline char *stpecpy(char *dst, char *end, const char *restrict src); * * end Pointer to one after the last element of the buffer * pointed to by `dst`. Usually, it should be calculated - * as `dst + NITEMS(dst)`. + * as `dst + countof(dst)`. * * src Source string to be copied into dst. * diff --git a/lib/string/strcpy/strncat.h b/lib/string/strcpy/strncat.h index 6bb9907bb..c88d89285 100644 --- a/lib/string/strcpy/strncat.h +++ b/lib/string/strcpy/strncat.h @@ -13,7 +13,7 @@ #include "sizeof.h" -#define STRNCAT(dst, src) strncat(dst, src, NITEMS(src)) +#define STRNCAT(dst, src) strncat(dst, src, countof(src)) #endif // include guard diff --git a/lib/string/strcpy/strncpy.h b/lib/string/strcpy/strncpy.h index fcb8720e2..391d0d3f0 100644 --- a/lib/string/strcpy/strncpy.h +++ b/lib/string/strcpy/strncpy.h @@ -13,7 +13,7 @@ #include "sizeof.h" -#define STRNCPY(dst, src) strncpy(dst, src, NITEMS(dst)) +#define STRNCPY(dst, src) strncpy(dst, src, countof(dst)) #endif // include guard diff --git a/lib/string/strcpy/strtcpy.h b/lib/string/strcpy/strtcpy.h index 4d35e1b81..aa89f99c0 100644 --- a/lib/string/strcpy/strtcpy.h +++ b/lib/string/strcpy/strtcpy.h @@ -31,7 +31,7 @@ * at the buffer pointed to by dst. If the destination buffer, * isn't large enough to hold the copy, the resulting string is * truncated. The size of the buffer is calculated internally via - * NITEMS(). + * countof(). * * RETURN VALUE * -1 If this call truncated the resulting string. @@ -44,7 +44,7 @@ */ -#define STRTCPY(dst, src) strtcpy(dst, src, NITEMS(dst)) +#define STRTCPY(dst, src) strtcpy(dst, src, countof(dst)) ATTR_STRING(2) diff --git a/lib/string/strdup/strndupa.h b/lib/string/strdup/strndupa.h index 80b8c3dcd..844c76bae 100644 --- a/lib/string/strdup/strndupa.h +++ b/lib/string/strdup/strndupa.h @@ -20,7 +20,7 @@ #endif -#define STRNDUPA(s) strndupa(s, NITEMS(s)) +#define STRNDUPA(s) strndupa(s, countof(s)) #endif // include guard diff --git a/lib/string/strdup/xstrndup.h b/lib/string/strdup/xstrndup.h index 88263c300..8d93eb3f8 100644 --- a/lib/string/strdup/xstrndup.h +++ b/lib/string/strdup/xstrndup.h @@ -18,7 +18,7 @@ // Similar to strndup(3), but ensure that 's' is an array, and exit on ENOMEM. #define XSTRNDUP(s) \ ( \ - STRNCAT(strcpy(XMALLOC(strnlen(s, NITEMS(s)) + 1, char), ""), s) \ + STRNCAT(strcpy(XMALLOC(strnlen(s, countof(s)) + 1, char), ""), s) \ ) diff --git a/lib/string/strftime.h b/lib/string/strftime.h index 2c86e89d1..8e71237dd 100644 --- a/lib/string/strftime.h +++ b/lib/string/strftime.h @@ -14,7 +14,7 @@ // string format time -#define STRFTIME(dst, fmt, tm) strftime(dst, NITEMS(dst), fmt, tm) +#define STRFTIME(dst, fmt, tm) strftime(dst, countof(dst), fmt, tm) #endif // include guard diff --git a/lib/time/day_to_str.h b/lib/time/day_to_str.h index fe3308d8c..3feb089e2 100644 --- a/lib/time/day_to_str.h +++ b/lib/time/day_to_str.h @@ -16,7 +16,7 @@ #include "string/strcpy/strtcpy.h" -#define DAY_TO_STR(str, day) day_to_str(NITEMS(str), str, day) +#define DAY_TO_STR(str, day) day_to_str(countof(str), str, day) inline void day_to_str(size_t size, char buf[size], long day); diff --git a/lib/utmp.c b/lib/utmp.c index 09fc2c9eb..ff886354a 100644 --- a/lib/utmp.c +++ b/lib/utmp.c @@ -36,7 +36,7 @@ #ident "$Id$" -#define UTX_LINESIZE NITEMS(memberof(struct utmpx, ut_line)) +#define UTX_LINESIZE countof(memberof(struct utmpx, ut_line)) /* diff --git a/src/login_nopam.c b/src/login_nopam.c index b7a7bd54f..856da11d5 100644 --- a/src/login_nopam.c +++ b/src/login_nopam.c @@ -275,7 +275,7 @@ static const char *resolve_hostname (const char *string) addr_str = host; gai_err = getnameinfo(addrs[0].ai_addr, addrs[0].ai_addrlen, - host, NITEMS(host), NULL, 0, NI_NUMERICHOST); + host, countof(host), NULL, 0, NI_NUMERICHOST); if (gai_err != 0) { SYSLOG ((LOG_ERR, "getnameinfo(%s): %s", string, gai_strerror(gai_err))); addr_str = string; diff --git a/tests/unit/test_snprintf.c b/tests/unit/test_snprintf.c index bdef8ac9a..b5144a61b 100644 --- a/tests/unit/test_snprintf.c +++ b/tests/unit/test_snprintf.c @@ -36,7 +36,7 @@ main(void) static void test_SNPRINTF_trunc(void **state) { - char buf[NITEMS("foo")]; + char buf[countof("foo")]; // Test that we're not returning SIZE_MAX assert_true(SNPRINTF(buf, "f%su", "oo") < 0); @@ -50,7 +50,7 @@ test_SNPRINTF_trunc(void **state) static void test_SNPRINTF_ok(void **state) { - char buf[NITEMS("foo")]; + char buf[countof("foo")]; assert_true(SNPRINTF(buf, "%s", "foo") == strlen("foo")); assert_true(strcmp(buf, "foo") == 0); diff --git a/tests/unit/test_strtcpy.c b/tests/unit/test_strtcpy.c index c27d7c9db..edf8fc9f3 100644 --- a/tests/unit/test_strtcpy.c +++ b/tests/unit/test_strtcpy.c @@ -37,7 +37,7 @@ main(void) static void test_STRTCPY_trunc(void **state) { - char buf[NITEMS("foo")]; + char buf[countof("foo")]; // Test that we're not returning SIZE_MAX assert_true(STRTCPY(buf, "fooo") < 0); @@ -51,7 +51,7 @@ test_STRTCPY_trunc(void **state) static void test_STRTCPY_ok(void **state) { - char buf[NITEMS("foo")]; + char buf[countof("foo")]; assert_int_equal(STRTCPY(buf, "foo"), strlen("foo")); assert_string_equal(buf, "foo");