[PATCH 13/13] flex-array: warn when a flexible array member has some padding

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If some padding is added because of the presence of a flexible
array member, the size of the structure will be greater than
the offset of this flexible array which can cause some
problems if the assumption is made that these 2 size must be
identical (which is easy to do since such flexible arrays are
conceptually 'after' the structure itself).

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 options.c                       | 2 ++
 options.h                       | 1 +
 sparse.1                        | 6 ++++++
 symbol.c                        | 2 ++
 validation/flex-array-padding.c | 1 -
 5 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/options.c b/options.c
index b46900b973a6..92d64e6ab64e 100644
--- a/options.c
+++ b/options.c
@@ -102,6 +102,7 @@ int Wenum_mismatch = 1;
 int Wexternal_function_has_definition = 1;
 int Wflexible_array_array = 1;
 int Wflexible_array_nested = 0;
+int Wflexible_array_padding = 0;
 int Wflexible_array_sizeof = 0;
 int Wimplicit_int = 1;
 int Winit_cstring = 0;
@@ -845,6 +846,7 @@ static const struct flag warnings[] = {
 	{ "external-function-has-definition", &Wexternal_function_has_definition },
 	{ "flexible-array-array", &Wflexible_array_array },
 	{ "flexible-array-nested", &Wflexible_array_nested },
+	{ "flexible-array-padding", &Wflexible_array_padding },
 	{ "flexible-array-sizeof", &Wflexible_array_sizeof },
 	{ "implicit-int", &Wimplicit_int },
 	{ "init-cstring", &Winit_cstring },
diff --git a/options.h b/options.h
index d23ed472eaac..31d9c5859977 100644
--- a/options.h
+++ b/options.h
@@ -101,6 +101,7 @@ extern int Wenum_mismatch;
 extern int Wexternal_function_has_definition;
 extern int Wflexible_array_array;
 extern int Wflexible_array_nested;
+extern int Wflexible_array_padding;
 extern int Wflexible_array_sizeof;
 extern int Wimplicit_int;
 extern int Winit_cstring;
diff --git a/sparse.1 b/sparse.1
index 9b1a59c6b9d4..8c61e869dc57 100644
--- a/sparse.1
+++ b/sparse.1
@@ -268,6 +268,12 @@ Sparse issues these warnings by default. To turn them off, use
 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-padding
+Warn about padding alignments caused by the presence of a flexible array member.
+
 Sparse does not issue these warnings by default.
 .
 .TP
diff --git a/symbol.c b/symbol.c
index a9f646eb053f..eabb2226651f 100644
--- a/symbol.c
+++ b/symbol.c
@@ -213,6 +213,8 @@ static struct symbol * examine_struct_union_type(struct symbol *sym, int advance
 	}
 	if (info.flex_array) {
 		info.has_flex_array = 1;
+		if (sym->offset != bits_to_bytes(bit_size) && Wflexible_array_padding)
+			warning(info.flex_array->pos, "flexible array member has padding");
 	}
 	if (info.has_flex_array)
 		sym->has_flex_array = 1;
diff --git a/validation/flex-array-padding.c b/validation/flex-array-padding.c
index 2ba77971266e..95b349e1199a 100644
--- a/validation/flex-array-padding.c
+++ b/validation/flex-array-padding.c
@@ -12,7 +12,6 @@ static int foo(struct s *s)
 /*
  * check-name: flex-array-padding
  * check-command: test-linearize -Wflexible-array-padding $file
- * check-known-to-fail
  *
  * check-output-ignore
  *
-- 
2.28.0




[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux