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: <https://stackoverflow.com/a/57537491>
Cc: Christian Göttsche <cgzones@googlemail.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2023-09-01 09:39:23 +02:00
committed by Iker Pedrosa
parent c3a8d02b9f
commit 49ea7327d9
+3 -1
View File
@@ -12,9 +12,11 @@
#include <limits.h>
#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)