chiark / gitweb /
basic: make sure argument of ELEMENTSOF is an array
authorMichal Schmidt <mschmidt@redhat.com>
Mon, 14 Sep 2015 18:32:17 +0000 (20:32 +0200)
committerSven Eden <yamakuzure@gmx.net>
Wed, 29 Mar 2017 08:45:09 +0000 (10:45 +0200)
Using ELEMENTSOF on a pointer will result in a compilation error.

src/basic/macro.h

index b9a24f6..e93d7f1 100644 (file)
@@ -124,8 +124,11 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
         return 1UL << (sizeof(u) * 8 - __builtin_clzl(u - 1UL));
 }
 
-#define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
-
+#define ELEMENTSOF(x)                                                    \
+        __extension__ (__builtin_choose_expr(                            \
+                !__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \
+                sizeof(x)/sizeof((x)[0]),                                \
+                (void)0))
 /*
  * container_of - cast a member of a structure out to the containing structure
  * @ptr: the pointer to the member.