[PATCH 2/2] vla-sizeof: add support for sizeof of VLAs

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

 



While sparse can parse VLA, their size were left as
'undeterminated' (-1) and trying to use sizeof on VLAs
resulted in a warning 'error: cannot size the expression'.

Fix this by adding the needing code to evaluate the expressions
corresponding to the sizeof of VLAs.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 evaluate.c               | 32 ++++++++++++++++++++++++++++++++
 validation/vla-sizeof1.c |  1 -
 validation/vla-sizeof2.c |  1 -
 validation/vla-sizeof3.c |  1 -
 validation/vla-sizeof4.c |  1 -
 5 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/evaluate.c b/evaluate.c
index 4e1dffe9c..a8d695de6 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -2208,6 +2208,38 @@ static struct symbol *evaluate_sizeof(struct expression *expr)
 		size = bits_in_char;
 	}
 
+	if (is_array_type(type) && size < 0) {	// VLA, 1-dimension only
+		struct expression *base, *size;
+		struct symbol *base_type;
+
+		if (type->type == SYM_NODE)
+			type = type->ctype.base_type;	// strip the SYM_NODE
+		base_type = get_base_type(type);
+		if (!base_type)
+			goto error;
+		if (base_type->bit_size <= 0) {
+			base = alloc_expression(expr->pos, EXPR_SIZEOF);
+			base->cast_type = base_type;
+			if (!evaluate_sizeof(base))
+				goto error;
+		} else {
+			base = alloc_expression(expr->pos, EXPR_VALUE);
+			base->value = bits_to_bytes(base_type->bit_size);
+			base->ctype = size_t_ctype;
+		}
+		size = alloc_expression(expr->pos, EXPR_CAST);
+		size->cast_type = size_t_ctype;
+		size->cast_expression = type->array_size;
+		if (!evaluate_expression(size))
+			goto error;
+		expr->left = size;
+		expr->right = base;
+		expr->type = EXPR_BINOP;
+		expr->op = '*';
+		return expr->ctype = size_t_ctype;
+	}
+
+error:
 	if ((size < 0) || (size & (bits_in_char - 1)))
 		expression_error(expr, "cannot size expression");
 
diff --git a/validation/vla-sizeof1.c b/validation/vla-sizeof1.c
index f0e32b7c1..ed7f5d4ee 100644
--- a/validation/vla-sizeof1.c
+++ b/validation/vla-sizeof1.c
@@ -11,7 +11,6 @@ static unsigned int foo(int x)
 /*
  * check-name: vla-sizeof var,cte
  * check-command: test-linearize -Wvla $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-contains: ret\\.32 *\\$1
diff --git a/validation/vla-sizeof2.c b/validation/vla-sizeof2.c
index b0b79a031..57927d169 100644
--- a/validation/vla-sizeof2.c
+++ b/validation/vla-sizeof2.c
@@ -11,7 +11,6 @@ static unsigned long foo(int x)
 /*
  * check-name: vla-sizeof var
  * check-command: test-linearize -Wvla $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-contains: ret\\..*\\$1
diff --git a/validation/vla-sizeof3.c b/validation/vla-sizeof3.c
index b9cb6acc2..21edb0078 100644
--- a/validation/vla-sizeof3.c
+++ b/validation/vla-sizeof3.c
@@ -11,7 +11,6 @@ static unsigned long foo(int x)
 /*
  * check-name: vla-sizeof var X cte
  * check-command: test-linearize -Wvla $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-contains: ret\\..*\\$1
diff --git a/validation/vla-sizeof4.c b/validation/vla-sizeof4.c
index 4b50fb2cb..e7478613d 100644
--- a/validation/vla-sizeof4.c
+++ b/validation/vla-sizeof4.c
@@ -11,7 +11,6 @@ static unsigned long foo(int x, int y)
 /*
  * check-name: vla-sizeof var X var
  * check-command: test-linearize -Wvla $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-contains: ret\\..*\\$1
-- 
2.16.2

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[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