diff mbox series

[11/17] fix expansion of initializer (mismatching size)

Message ID 20191210225921.94897-12-luc.vanoostenryck@gmail.com (mailing list archive)
State Mainlined, archived
Headers show
Series improve expansion of constant symbols | expand

Commit Message

Luc Van Oostenryck Dec. 10, 2019, 10:59 p.m. UTC
Currently, the expansion of constant initializers is done
whenever the offset in the initializer match the one
we're expanding.

However, it's not correct to do this expansion if their
size doesn't match since in this case the value of one
doesn't represent the value of the other.

Fix this by adding a check for the size.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 expand.c                                | 8 ++++----
 validation/expand/constant-union-size.c | 1 -
 2 files changed, 4 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/expand.c b/expand.c
index ae764153d3e7..04b072c53b5a 100644
--- a/expand.c
+++ b/expand.c
@@ -625,8 +625,6 @@  static int expand_addressof(struct expression *expr)
  * Look up a trustable initializer value at the requested offset.
  *
  * Return NULL if no such value can be found or statically trusted.
- *
- * FIXME!! We should check that the size is right!
  */
 static struct expression *constant_symbol_value(struct symbol *sym, int offset)
 {
@@ -688,11 +686,13 @@  static int expand_dereference(struct expression *expr)
 
 	if (unop->type == EXPR_SYMBOL) {
 		struct symbol *sym = unop->symbol;
+		struct symbol *ctype = expr->ctype;
 		struct expression *value = constant_symbol_value(sym, offset);
 
 		/* Const symbol with a constant initializer? */
-		if (value) {
-			/* FIXME! We should check that the size is right! */
+		if (value && value->ctype) {
+			if (ctype->bit_size != value->ctype->bit_size)
+				return UNSAFE;
 			if (value->type == EXPR_VALUE) {
 				if (is_bitfield_type(value->ctype))
 					return UNSAFE;
diff --git a/validation/expand/constant-union-size.c b/validation/expand/constant-union-size.c
index b6c3ac75ae4b..8a16bf3e8033 100644
--- a/validation/expand/constant-union-size.c
+++ b/validation/expand/constant-union-size.c
@@ -13,7 +13,6 @@  static int foo(void)
  * check-name: constant-union-size
  * check description: the size of the initializer doesn't match
  * check-command: test-linearize -fdump-ir $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-contains: load\\.