sparse: add -Wpointer-arith flag to toggle sizeof(void) warnings
PATCH v3 1/1 sparse: add -Wpointer-arith flag to toggle sizeof(void) warnings
Recent changes to the min()/max() macros in include/linux/kernel.h have added a lot of noise when compiling the kernel with Sparse checking enabled. This mostly is due to the *huge* increase in the number of sizeof(void) warnings, a larger number of which can safely be ignored.
Add the -Wpointer-arith flag to enable/disable these warnings (along with the warning when applying sizeof to function types as well as warning about pointer arithmetic on these types exactly like the GCC -Wpointer-arith flag) on demand; the warning itself has been disabled by default to reduce the large influx of noise which was inadvertently added by commit 3c8ba0d61d04ced9f8 (kernel.h: Retain constant expression output for max()/min()).
Update the manpage to document the new flag and add/update validation cases regarding the application of sizeof to void and function types.
evaluate.c | 6 ++++-- lib.c | 2 ++ lib.h | 1 + sparse.1 | 13 ++++++++++++ validation/sizeof-function.c | 2 +- validation/sizeof-void.c | 39 ++++++++++++++++++++++++++++++++++++ 6 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 validation/sizeof-void.c
6 files changed, 60 insertions(+), 3 deletions(-)
evaluate.c +4 -2
@@ -2193,7 +2193,8 @@ static struct symbol *evaluate_sizeof(struct expression *expr) size = type->bit_size;if (size <0 &&is_void_type (type)) { -warning (expr->pos,"expression using sizeof(void)" ); +if (Wpointer_arith) +warning (expr->pos,"expression using sizeof(void)" ); size = bits_in_char; }
@@ -2204,7 +2205,8 @@ static struct symbol *evaluate_sizeof(struct expression *expr) }if (is_function (type->ctype.base_type)) { -warning (expr->pos,"expression using sizeof on a function" ); +if (Wpointer_arith) +warning (expr->pos,"expression using sizeof on a function" ); size = bits_in_char; }
lib.c +2 -0
@@ -242,6 +242,7 @@ int Woverride_init = 1;int Woverride_init_all =0 ;int Woverride_init_whole_range =0 ;int Wparen_string =0 ; +int Wpointer_arith =0 ;int Wptr_subtraction_blows =0 ;int Wreturn_void =0 ;int Wshadow =0 ;
@@ -654,6 +655,7 @@ static const struct flag warnings[] = { { "return-void", &Wreturn_void }, { "shadow", &Wshadow }, { "sizeof-bool", &Wsizeof_bool }, + { "pointer-arith", &Wpointer_arith }, { "sparse-error", &Wsparse_error }, { "tautological-compare", &Wtautological_compare }, { "transparent-union", &Wtransparent_union },
lib.h +1 -0
@@ -151,6 +151,7 @@ extern int Woverride_init;extern int Woverride_init_all;extern int Woverride_init_whole_range;extern int Wparen_string; +extern int Wpointer_arith;extern int Wptr_subtraction_blows;extern int Wreturn_void;extern int Wshadow;
sparse.1 +13 -0
@@ -288,6 +288,19 @@ Standard C syntax does not permit a parenthesized string as an array initializer. GCC allows this syntax as an extension. With \fB\-Wparen\-string\fR, Sparse will warn about this syntax. +Sparse does not issue these warnings by default. +. +.TP +.B \-Wpointer\-arith +Warn about anything that depends on the \fBsizeof\fR a void or function type. + +C99 does not allow the \fBsizeof\fR operator to be applied to function types +or to incomplete types such as void. GCC allows \fBsizeof\fR to be applied to +these types as an extension and assigns these types a size of \fI1\fR. With +\fB\-pointer\-arith\fR, Sparse will warn about pointer arithmetic on void +or function pointers, as well as expressions which directly apply the +\fBsizeof\fR operator to void or function types. + Sparse does not issue these warnings by default. . .TP
validation/sizeof-function.c +1 -1
@@ -35,7 +35,7 @@ int test(void)/* * check-name: sizeof-function -* check-command: sparse -Wno-decl $file +* check-command: sparse -Wpointer-arith -Wno-decl $file * * check-error-start sizeof-function.c:22:14: warning: expression using sizeof on a function
validation/sizeof-void.c new file +39 -0
@@ -0,0 +1,39 @@ +#define is_constexpr (x) \ + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) + +int test (void ) +{ +unsigned int s =0 , i =0 ; +void *ptr = &i; + +// OK + s +=sizeof i; + s +=sizeof &i; + s +=sizeof ptr; + s +=sizeof &ptr; + +// KO + s +=sizeof (void ); + s +=sizeof *ptr; + s +=is_constexpr (1 +1 ); + s +=is_constexpr ((1 ,1 )); + s +=is_constexpr (ptr +1 ); + s +=is_constexpr (&ptr +1 ); + s +=is_constexpr (*(((char *)&ptr) +1 )); + +return s; +} + +/* +* check-name: sizeof-void +* check-command: sparse -Wpointer-arith -Wno-decl -Wno-unused-value $file +* +* check-error-start +sizeof-void.c:16:14: warning: expression using sizeof(void) +sizeof-void.c:17:14: warning: expression using sizeof(void) +sizeof-void.c:19:14: warning: expression using sizeof(void) +sizeof-void.c:20:14: warning: expression using sizeof(void) +sizeof-void.c:21:14: warning: expression using sizeof(void) +sizeof-void.c:22:14: warning: expression using sizeof(void) +* check-error-end +*/
Signature · 1 line
2.17.0.rc1.35.g90bbd502d54fe92035.dirty
Quoted 5 lines · Joey Pabalinas
Recent changes to the min()/max() macros in
include/linux/kernel.hhave added a lot of noise when compiling the kernel with Sparse checking enabled. This mostly is due to the *huge* increase in the number of sizeof(void) warnings, a larger number of which can safely be ignored.
Thank you very much.
There is just a problem with the test but it's my fault as I pointed to you to my tree but the master tree lack a lot of fixes and have problems when dereferencing function pointers. So, for the master tree, I propose to use a version with these tests removed:
git://github.com/lucvoo/sparse-dev.git pointer-arith-v3
Cheers,
-- Luc
Quoted 8 lines · Luc Van Oostenryck
Thank you very much.
There is just a problem with the test but it's my fault as I pointed to you to my tree but the master tree lack a lot of fixes and have problems when dereferencing function pointers. So, for the master tree, I propose to use a version with these tests removed:
git://github.com/lucvoo/sparse-dev.gitpointer-arith-v3
Not a problem; I will rebase it on the v0.5.2-rc1 tag and take out the function pointer test-case.
Signature · 2 lines
Cheers, Joey Pabalinas
Quoted 2 lines · Joey Pabalinas
Not a problem; I will rebase it on the v0.5.2-rc1 tag and take out the function pointer test-case.
Sorry, I meant v0.5.2, heh. Sending the v4 now.
Signature · 2 lines
Cheers, Joey Pabalinas