linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] force to 0 expressions which are erroneously non-constant
@ 2020-08-09 16:41 Luc Van Oostenryck
  0 siblings, 0 replies; 3+ messages in thread
From: Luc Van Oostenryck @ 2020-08-09 16:41 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

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.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 expand.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/expand.c b/expand.c
index 623b180025ad..c4f806dee1ba 100644
--- a/expand.c
+++ b/expand.c
@@ -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;
+		}
 	}
 }
 
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH] force to 0 expressions which are erroneously non-constant
@ 2020-08-10  5:51 Luc Van Oostenryck
  0 siblings, 0 replies; 3+ messages in thread
From: Luc Van Oostenryck @ 2020-08-10  5:51 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

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.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 expand.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/expand.c b/expand.c
index 623b180025ad..c4f806dee1ba 100644
--- a/expand.c
+++ b/expand.c
@@ -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;
+		}
 	}
 }
 
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH] force to 0 expressions which are erroneously non-constant
@ 2017-05-31 13:31 Luc Van Oostenryck
  0 siblings, 0 replies; 3+ messages in thread
From: Luc Van Oostenryck @ 2017-05-31 13:31 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

Wher some expression that need to be constant but in fact is not
constant, sparse throw an error and leave the expression as-is.

But some code may make the explicit or implicit assumption that
the expression is constant and use its value, resulting in some
random value, which is not desirable.

One situation where this happen 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
benefit.

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

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 expand.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/expand.c b/expand.c
index dacee7a76..8ee52cfd5 100644
--- a/expand.c
+++ b/expand.c
@@ -1054,8 +1054,11 @@ 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->type = EXPR_VALUE;
+			expr->value = 0;
+		}
 	}
 }
 
-- 
2.13.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-08-10  5:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-09 16:41 [PATCH] force to 0 expressions which are erroneously non-constant Luc Van Oostenryck
  -- strict thread matches above, loose matches on Subject: below --
2020-08-10  5:51 Luc Van Oostenryck
2017-05-31 13:31 Luc Van Oostenryck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).