diff options
| author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2020-09-30 23:05:16 +0200 |
|---|---|---|
| committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2020-10-01 00:45:21 +0200 |
| commit | 1a88f2f24619e7a93aaef917fc72eba71706c9df (patch) | |
| tree | 03d41386a00e539281647d67562b0343f6142bd2 | |
| parent | 58721d6e25e1845ce147150755e61401fcbc1a32 (diff) | |
| download | sparse-dev-1a88f2f24619e7a93aaef917fc72eba71706c9df.tar.gz | |
flex-array: warn when using sizeof() on a flexible array
Using sizeof() on a structure containing a flexible array
will ignore the 'flexible' part. This is maybe what is expected
but maybe not, so add an option -Wflexible-array-sizeof to
warn on such usage.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
| -rw-r--r-- | evaluate.c | 3 | ||||
| -rw-r--r-- | options.c | 2 | ||||
| -rw-r--r-- | options.h | 1 | ||||
| -rw-r--r-- | sparse.1 | 7 | ||||
| -rw-r--r-- | validation/flex-array-sizeof.c | 1 |
5 files changed, 13 insertions, 1 deletions
@@ -2253,6 +2253,9 @@ static struct symbol *evaluate_sizeof(struct expression *expr) size = bits_in_char; } + if (has_flexible_array(type) && Wflexible_array_sizeof) + warning(expr->pos, "using sizeof on a flexible structure"); + if (is_array_type(type) && size < 0) { // VLA, 1-dimension only struct expression *base, *size; struct symbol *base_type; @@ -100,6 +100,7 @@ int Wdesignated_init = 1; int Wdo_while = 0; int Wenum_mismatch = 1; int Wexternal_function_has_definition = 1; +int Wflexible_array_sizeof = 0; int Wimplicit_int = 1; int Winit_cstring = 0; int Wint_to_pointer_cast = 1; @@ -840,6 +841,7 @@ static const struct flag warnings[] = { { "do-while", &Wdo_while }, { "enum-mismatch", &Wenum_mismatch }, { "external-function-has-definition", &Wexternal_function_has_definition }, + { "flexible-array-sizeof", &Wflexible_array_sizeof }, { "implicit-int", &Wimplicit_int }, { "init-cstring", &Winit_cstring }, { "int-to-pointer-cast", &Wint_to_pointer_cast }, @@ -99,6 +99,7 @@ extern int Wdesignated_init; extern int Wdo_while; extern int Wenum_mismatch; extern int Wexternal_function_has_definition; +extern int Wflexible_array_sizeof; extern int Wimplicit_int; extern int Winit_cstring; extern int Wint_to_pointer_cast; @@ -257,6 +257,13 @@ Sparse issues these warnings by default. To turn them off, use \fB\-Wno\-external\-function\-has\-definition\fR. . .TP +.B -Wflexible-array-sizeof +Warn about using the sizeof operator on a structure containing a flexible array, +possibly recursively. + +Sparse does not issue these warnings by default. +. +.TP .B \-Winit\-cstring Warn about initialization of a char array with a too long constant C string. diff --git a/validation/flex-array-sizeof.c b/validation/flex-array-sizeof.c index 3359509d..05394e19 100644 --- a/validation/flex-array-sizeof.c +++ b/validation/flex-array-sizeof.c @@ -11,7 +11,6 @@ static int foo(struct s *s) /* * check-name: flex-array-sizeof * check-command: sparse -Wflexible-array-sizeof $file - * check-known-to-fail * * check-error-start flex-array-sizeof.c:8:16: warning: using sizeof on a flexible structure |
