]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
sizeof.h: Make NITEMS() and derivative macros safe against pointers
authorAlejandro Colomar <alx@kernel.org>
Sun, 30 Jul 2023 12:26:27 +0000 (14:26 +0200)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Fri, 1 Sep 2023 07:39:23 +0000 (09:39 +0200)
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>
lib/sizeof.h

index b1a5daf11d17ea591d7766b1b0a56ba86cb89546..0ee0667a19a4c11d9dbe2d3d446f04746f1e88dc 100644 (file)
 
 #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)