aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
-rw-r--r--options.c2
-rw-r--r--options.h1
-rw-r--r--sparse.19
-rw-r--r--symbol.c2
-rw-r--r--validation/flex-array-union-array-no.c9
-rw-r--r--validation/flex-array-union-array-yes.c11
-rw-r--r--validation/flex-array-union-array.h11
7 files changed, 44 insertions, 1 deletions
diff --git a/options.c b/options.c
index b46900b9..e2a0717a 100644
--- a/options.c
+++ b/options.c
@@ -103,6 +103,7 @@ int Wexternal_function_has_definition = 1;
int Wflexible_array_array = 1;
int Wflexible_array_nested = 0;
int Wflexible_array_sizeof = 0;
+int Wflexible_array_union = 0;
int Wimplicit_int = 1;
int Winit_cstring = 0;
int Wint_to_pointer_cast = 1;
@@ -846,6 +847,7 @@ static const struct flag warnings[] = {
{ "flexible-array-array", &Wflexible_array_array },
{ "flexible-array-nested", &Wflexible_array_nested },
{ "flexible-array-sizeof", &Wflexible_array_sizeof },
+ { "flexible-array-union", &Wflexible_array_union },
{ "implicit-int", &Wimplicit_int },
{ "init-cstring", &Winit_cstring },
{ "int-to-pointer-cast", &Wint_to_pointer_cast },
diff --git a/options.h b/options.h
index d23ed472..70c6ce9b 100644
--- a/options.h
+++ b/options.h
@@ -102,6 +102,7 @@ extern int Wexternal_function_has_definition;
extern int Wflexible_array_array;
extern int Wflexible_array_nested;
extern int Wflexible_array_sizeof;
+extern int Wflexible_array_union;
extern int Wimplicit_int;
extern int Winit_cstring;
extern int Wint_to_pointer_cast;
diff --git a/sparse.1 b/sparse.1
index 9b1a59c6..ed528fd1 100644
--- a/sparse.1
+++ b/sparse.1
@@ -278,6 +278,15 @@ possibly recursively.
Sparse does not issue these warnings by default.
.
.TP
+.B -Wflexible-array-union
+Enable the warnings regarding flexible arrays and unions.
+To have any effect, at least one of \fB-Wflexible-array-array\fR,
+\fB-Wflexible-array-nested\fR or \fB-Wflexible-array-sizeof\fR must also
+be enabled.
+
+Sparse does 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/symbol.c b/symbol.c
index 97c2e35d..9ae827c1 100644
--- a/symbol.c
+++ b/symbol.c
@@ -214,7 +214,7 @@ static struct symbol * examine_struct_union_type(struct symbol *sym, int advance
if (info.flex_array) {
info.has_flex_array = 1;
}
- if (info.has_flex_array)
+ if (info.has_flex_array && (!is_union_type(sym) || Wflexible_array_union))
sym->has_flex_array = 1;
sym->bit_size = bit_size;
return sym;
diff --git a/validation/flex-array-union-array-no.c b/validation/flex-array-union-array-no.c
new file mode 100644
index 00000000..5a1de787
--- /dev/null
+++ b/validation/flex-array-union-array-no.c
@@ -0,0 +1,9 @@
+#include "flex-array-union-array.h"
+
+/*
+ * check-name: flex-array-union-no
+ * check-command: sparse -Wflexible-array-array -Wno-flexible-array-union $file
+ *
+ * check-error-start
+ * check-error-end
+ */
diff --git a/validation/flex-array-union-array-yes.c b/validation/flex-array-union-array-yes.c
new file mode 100644
index 00000000..c2b71d65
--- /dev/null
+++ b/validation/flex-array-union-array-yes.c
@@ -0,0 +1,11 @@
+#include "flex-array-union-array.h"
+
+/*
+ * check-name: flex-array-union-yes
+ * check-command: sparse -Wflexible-array-array -Wflexible-array-union $file
+ *
+ * check-error-start
+flex-array-union-array-yes.c: note: in included file:
+flex-array-union-array.h:11:17: warning: array of flexible structures
+ * check-error-end
+ */
diff --git a/validation/flex-array-union-array.h b/validation/flex-array-union-array.h
new file mode 100644
index 00000000..b2a74d1a
--- /dev/null
+++ b/validation/flex-array-union-array.h
@@ -0,0 +1,11 @@
+struct s_flex {
+ int i;
+ long f[];
+};
+
+union s {
+ struct s_flex flex;
+ char buf[200];
+};
+
+static union s a[2];