Skip to contentSkip to conversation

sparse: add -Wpointer-arith flag to toggle sizeof(void) warnings

PATCH In review Joey Pabalinas 2 participants 2 messages Apr 10, 2018 – Apr 11, 2018 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH v4 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 a validation case for sizeof(void).

CC Kees Cook <[email protected]>
CC Linus Torvalds <[email protected]>
CC Martin Uecker <[email protected]>
CC Al Viro <[email protected]>
CC Christopher Li <[email protected]>
CC Joey Pabalinas <[email protected]>
CC Luc Van Oostenryck <[email protected]>
 5 files changed, 64 insertions(+), 2 deletions(-)
evaluate.c +4 -2
index b96696d3a51396800a..8f07d08cf5b494f8f0 100644
@@ -2169,7 +2169,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;
 	}
 
@@ -2180,7 +2181,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
index 645132a892107512a1..0caee38a996cd0fa4c 100644
@@ -241,6 +241,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;
@@ -536,6 +537,7 @@ static const struct warning {
 	{ "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
index a9b70b07686801305c..98e20d3830d9059acb 100644
@@ -134,6 +134,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
index e183204de623efd022..3bd5f1cb11309e65b8 100644
@@ -282,6 +282,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-void.c new file +44 -0

44 lines not on this page · raw message

Signature · 1 line
2.17.0.rc1.35.g90bbd502d54fe92035.dirty
Acked-by on v4 1/1
#1 lore raw
Quoted 16 lines · Joey Pabalinas

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 a validation case for sizeof(void).

Quoted 9 lines
CC Kees Cook <[email protected]>
CC Linus Torvalds <[email protected]>
CC Martin Uecker <[email protected]>
CC Al Viro <[email protected]>
CC Christopher Li <[email protected]>
CC Joey Pabalinas <[email protected]>
CC Luc Van Oostenryck <[email protected]>

Thanks,