From 49ea7327d96d1f5793fdd9b5c2bd1d6e252b3f6b Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 30 Jul 2023 14:26:27 +0200 Subject: [PATCH] sizeof.h: Make NITEMS() and derivative macros safe against pointers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- lib/sizeof.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/sizeof.h b/lib/sizeof.h index b1a5daf1..0ee0667a 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)