Skip to contentSkip to conversation

force to 0 expressions which are erroneously non-constant

PATCH New Luc Van Oostenryck 1 participant 1 message Aug 10, 2020 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 force to 0 expressions which are erroneously non-constant

When an expression that needs to be constant but is, in fact, not constant, sparse throws an error and leaves it as-is. But some code makes the assumption that the expression is constant and uses its value, with some random result.

One situation where this happens is in code like:

	switch (x) {
	case <some non-const expression>: ...

In this case, the linearization of the switch/case statement will unconditionally use the value of the case expression but the expression has no value.

One way to avoid this would be to add defensive checks each time a value is retrieved but this is a lot of work and time for no benefits.

So, change this by forcing the expression to be a constant value of 0 just after the error message has been issued.

---
 expand.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
expand.c +5 -1
index 623b180025ad..c4f806dee1ba 100644
@@ -1177,8 +1177,12 @@ static void expand_const_expression(struct expression *expr, const char *where)
 {
 	if (expr) {
 		expand_expression(expr);
-		if (expr->type != EXPR_VALUE)
+		if (expr->type != EXPR_VALUE) {
 			expression_error(expr, "Expected constant expression in %s", where);
+			expr->ctype = &int_ctype;
+			expr->type = EXPR_VALUE;
+			expr->value = 0;
+		}
 	}
 }
 
Signature · 1 line
2.28.0