Skip to contentSkip to conversation

fix sparse problems with ARRAY_SIZE

PATCH In review Adel Gadllah 3 participants 6 messages Jul 10, 2007 – Jul 12, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix sparse problems with ARRAY_SIZE

ARRAY_SIZE seems to create sparse warnings when used on global variables. The attached patch fixes this by using a ARRAY_SIZE macro that does not use __must_be_array(arr) for sparse.
See: http://marc.info/?t=118103449100007&r=1&w=2

P.S: please CC me

include/linux/kernel.h +4 -0
diff -upNr linux-2.6.orig/include/linux/kernel.h linux-2.6/include/linux/kernel.h
@@ -35,7 +35,11 @@ extern const char linux_proc_banner[];
 #define ALIGN(x,a)		__ALIGN_MASK(x,(typeof(x))(a)-1)
 #define __ALIGN_MASK(x,mask)	(((x)+(mask))&~(mask))
 
+#ifdef __CHECKER_
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((x)[0]))
+#else
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
+#endif
 
 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#1 lore raw
Quoted 4 lines · Adel Gadllah

ARRAY_SIZE seems to create sparse warnings when used on global variables. The attached patch fixes this by using a ARRAY_SIZE macro that does not use __must_be_array(arr) for sparse.
See: http://marc.info/?t=118103449100007&r=1&w=2

P.S: please CC me

resend the working one sorry.

include/linux/kernel.h +4 -0
diff -upNr linux-2.6.orig/include/linux/kernel.h linux-2.6/include/linux/kernel.h
@@ -35,7 +35,11 @@ extern const char linux_proc_banner[];
 #define ALIGN(x,a)		__ALIGN_MASK(x,(typeof(x))(a)-1)
 #define __ALIGN_MASK(x,mask)	(((x)+(mask))&~(mask))
 
+#ifdef __CHECKER_
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#else
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
+#endif
 
 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#2 lore raw

include/linux/kernel.h extern const char linux_proc_banner[]; · line 42

Quoted 11 lines · Adel Gadllah
include/linux/kernel.h +4 -0
@@ -35,7 +35,11 @@ extern const char linux_proc_banner[];
 #define ALIGN(x,a)		__ALIGN_MASK(x,(typeof(x))(a)-1)
 #define __ALIGN_MASK(x,mask)	(((x)+(mask))&~(mask))
 
+#ifdef __CHECKER_
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#else
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
+#endif

Al Viro recently gave a hint that he have patches for sparse to not give spurious ARRAY_SIZE warnings.

#3 lore raw
Quoted 7 lines · Alexey Dobriyan
Quoted 6 lines · Adel Gadllah
include/linux/kernel.h +0 -0
@@ -35,7 +35,11 @@ extern const char linux_proc_banner[];
 #define ALIGN(x,a)		__ALIGN_MASK(x,(typeof(x))(a)-1)
 #define __ALIGN_MASK(x,mask)	(((x)+(mask))&~(mask))
 
Quoted 8 lines

include/linux/kernel.h extern const char linux_proc_banner[]; · line 42

Quoted 5 lines
include/linux/kernel.h +4 -0
+#ifdef __CHECKER_
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#else
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
+#endif

Al Viro recently gave a hint that he have patches for sparse to not give spurious ARRAY_SIZE warnings.

I do. Right now it's either 2 or 4 patches away from mainline sparse, depending on reordering... I'm splitting the last patch in series, hopefully will reorder and send tonight.

Basically, it's a matter of getting __builtin_compatible_types_p() implementation (really - evaluate.c:type_difference()) somewhere sane.

#4 lore raw
Quoted 17 lines · Al Viro
Quoted 7 lines · Alexey Dobriyan
Quoted 6 lines · Adel Gadllah
include/linux/kernel.h +0 -0
@@ -35,7 +35,11 @@ extern const char linux_proc_banner[];
 #define ALIGN(x,a)		__ALIGN_MASK(x,(typeof(x))(a)-1)
 #define __ALIGN_MASK(x,mask)	(((x)+(mask))&~(mask))
 
Quoted 8 lines

include/linux/kernel.h extern const char linux_proc_banner[]; · line 42

Quoted 5 lines
include/linux/kernel.h +4 -0
+#ifdef __CHECKER_
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#else
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
+#endif

Al Viro recently gave a hint that he have patches for sparse to not give spurious ARRAY_SIZE warnings.

Quoted 6 lines

I do. Right now it's either 2 or 4 patches away from mainline sparse, depending on reordering... I'm splitting the last patch in series, hopefully will reorder and send tonight.

Basically, it's a matter of getting __builtin_compatible_types_p() implementation (really - evaluate.c:type_difference()) somewhere sane.

Try git://git.kernel.org/pub/scm/linux/kernel/git/viro/sparse.git/,
the branch is type_difference

#5 lore raw
Quoted 25 lines · Al Viro
Quoted 23 lines · Al Viro
Quoted 8 lines · Alexey Dobriyan
Quoted 6 lines · Adel Gadllah
include/linux/kernel.h +0 -0
@@ -35,7 +35,11 @@ extern const char linux_proc_banner[];
 #define ALIGN(x,a)		__ALIGN_MASK(x,(typeof(x))(a)-1)
 #define __ALIGN_MASK(x,mask)	(((x)+(mask))&~(mask))
 
Quoted 9 lines

include/linux/kernel.h extern const char linux_proc_banner[]; · line 43

Quoted 6 lines
include/linux/kernel.h +4 -0
+#ifdef __CHECKER_
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#else
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
+#endif
 

Al Viro recently gave a hint that he have patches for sparse to not give spurious ARRAY_SIZE warnings.

I do. Right now it's either 2 or 4 patches away from mainline sparse, depending on reordering... I'm splitting the last patch in series, hopefully will reorder and send tonight.

Quoted 8 lines
Quoted 3 lines

Basically, it's a matter of getting __builtin_compatible_types_p() implementation (really - evaluate.c:type_difference()) somewhere sane.

Try git://git.kernel.org/pub/scm/linux/kernel/git/viro/sparse.git/,
the branch is type_difference

ok a proper fix is always better than a workaround ;)
it seems that ARRAY_SIZE is defined multiple times....
seem wrong... shouldn't all users include linux/kernel.h and use that one?