aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2020-09-24 21:56:23 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2020-10-01 00:46:44 +0200
commit02ed28909f3b79e0ad1ca57b9699f39e1bb7cdcf (patch)
treece2af5f765a2960b40aa3a8f1e555ceec04d5611
parent8b187dd493fb934f38061d9ee265a888a6b4b776 (diff)
downloadsparse-dev-02ed28909f3b79e0ad1ca57b9699f39e1bb7cdcf.tar.gz
flex-array: warn on flexible array in nested aggregate types
A structure or a union containing another aggregate type containing, possibly recursively, a flexible array is quite error prone and make not much sense. So, add an option -Wflexible-array-nested to warn on such usage. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--options.c2
-rw-r--r--options.h1
-rw-r--r--sparse.17
-rw-r--r--symbol.c4
-rw-r--r--validation/flex-array-nested.c1
5 files changed, 14 insertions, 1 deletions
diff --git a/options.c b/options.c
index a8129ac1..b46900b9 100644
--- a/options.c
+++ b/options.c
@@ -101,6 +101,7 @@ int Wdo_while = 0;
int Wenum_mismatch = 1;
int Wexternal_function_has_definition = 1;
int Wflexible_array_array = 1;
+int Wflexible_array_nested = 0;
int Wflexible_array_sizeof = 0;
int Wimplicit_int = 1;
int Winit_cstring = 0;
@@ -843,6 +844,7 @@ static const struct flag warnings[] = {
{ "enum-mismatch", &Wenum_mismatch },
{ "external-function-has-definition", &Wexternal_function_has_definition },
{ "flexible-array-array", &Wflexible_array_array },
+ { "flexible-array-nested", &Wflexible_array_nested },
{ "flexible-array-sizeof", &Wflexible_array_sizeof },
{ "implicit-int", &Wimplicit_int },
{ "init-cstring", &Winit_cstring },
diff --git a/options.h b/options.h
index 7bcf925b..d23ed472 100644
--- a/options.h
+++ b/options.h
@@ -100,6 +100,7 @@ extern int Wdo_while;
extern int Wenum_mismatch;
extern int Wexternal_function_has_definition;
extern int Wflexible_array_array;
+extern int Wflexible_array_nested;
extern int Wflexible_array_sizeof;
extern int Wimplicit_int;
extern int Winit_cstring;
diff --git a/sparse.1 b/sparse.1
index c1a74153..9b1a59c6 100644
--- a/sparse.1
+++ b/sparse.1
@@ -264,6 +264,13 @@ Sparse issues these warnings by default. To turn them off, use
\fB-Wno-flexible-array-array\fR.
.
.TP
+.B -Wflexible-array-nested
+Warn about structures containing a flexible array being contained into
+another structure, union or array.
+
+Sparse does not issue these warnings by default.
+.
+.TP
.B -Wflexible-array-sizeof
Warn about using the sizeof operator on a structure containing a flexible array,
possibly recursively.
diff --git a/symbol.c b/symbol.c
index 02b9066e..a9f646eb 100644
--- a/symbol.c
+++ b/symbol.c
@@ -197,6 +197,10 @@ static struct symbol * examine_struct_union_type(struct symbol *sym, int advance
info.max_align = member->ctype.alignment;
}
+ if (has_flexible_array(member))
+ info.has_flex_array = 1;
+ if (has_flexible_array(member) && Wflexible_array_nested)
+ warning(sym->pos, "nested flexible arrays");
fn(member, &info);
} END_FOR_EACH_PTR(member);
diff --git a/validation/flex-array-nested.c b/validation/flex-array-nested.c
index 3503c329..63767683 100644
--- a/validation/flex-array-nested.c
+++ b/validation/flex-array-nested.c
@@ -21,7 +21,6 @@ static int foo(struct s *s, union u *u)
/*
* check-name: flex-array-nested
* check-command: sparse -Wflexible-array-nested $file
- * check-known-to-fail
*
* check-error-start
flex-array-nested.c:6:8: warning: nested flexible arrays