From: Alejandro Colomar Date: Sun, 30 Jul 2023 12:26:27 +0000 (+0200) Subject: sizeof.h: Make NITEMS() and derivative macros safe against pointers X-Git-Tag: 4.15.0-rc1~208 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=49ea7327d96d1f5793fdd9b5c2bd1d6e252b3f6b;p=thirdparty%2Fshadow.git sizeof.h: Make NITEMS() and derivative macros safe against pointers By using must_be_array(), code that calls NITEMS() or STRLEN() with non-arrays will not compile. Link: Cc: Christian Göttsche Cc: Serge Hallyn Cc: Iker Pedrosa Signed-off-by: Alejandro Colomar --- diff --git a/lib/sizeof.h b/lib/sizeof.h index b1a5daf11..0ee0667a1 100644 --- a/lib/sizeof.h +++ b/lib/sizeof.h @@ -12,9 +12,11 @@ #include +#include "must_be.h" + #define WIDTHOF(x) (sizeof(x) * CHAR_BIT) -#define NITEMS(arr) (sizeof((arr)) / sizeof((arr)[0])) +#define NITEMS(a) (sizeof((a)) / sizeof((a)[0]) + must_be_array(a)) #define STRLEN(s) (NITEMS(s) - 1)