[PATCH 4/6] context: fix crashes while parsing '__context__;' or '__context__(;'

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

 



The expected syntax for the __context__ statement is:
	__context__(<inc/dec value>);
or
	__context__(<context>, <inc/dec value>);

The distinction between the two formats is made by checking if
the expression is a PREOP with '(' as op and with an comma
expression as inner expression.

However, code like:
	__context__;
or
	__context__(;
crashes while trying to test the non-existing expression
(after PREOP or after the comma expression).

Fix this by testing if the expression is non-null before
dereferencing it.

Note: this fix has the merit to directly address the problem
      but doesn't let a diagnostic to be issued for the case
	__context__;
      which is considered as perfectly valid.
      The next patch will take care of this.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 parse.c                   | 4 +++-
 validation/context-stmt.c | 7 +++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/parse.c b/parse.c
index 68cdeb226..42b3fd20a 100644
--- a/parse.c
+++ b/parse.c
@@ -2339,8 +2339,10 @@ static struct token *parse_context_statement(struct token *token, struct stateme
 {
 	stmt->type = STMT_CONTEXT;
 	token = parse_expression(token->next, &stmt->expression);
-	if (stmt->expression->type == EXPR_PREOP
+	if (stmt->expression
+	    && stmt->expression->type == EXPR_PREOP
 	    && stmt->expression->op == '('
+	    && stmt->expression->unop
 	    && stmt->expression->unop->type == EXPR_COMMA) {
 		struct expression *expr;
 		expr = stmt->expression->unop;
diff --git a/validation/context-stmt.c b/validation/context-stmt.c
index cb85e562b..1f02c3a67 100644
--- a/validation/context-stmt.c
+++ b/validation/context-stmt.c
@@ -6,6 +6,9 @@ static void foo(int x)
 
 	__context__(x);		// KO: no const expr
 	__context__(1,x);	// KO: no const expr
+
+	__context__;		// KO: no expression at all
+	__context__(;		// KO: no expression at all
 }
 
 /*
@@ -13,7 +16,11 @@ static void foo(int x)
  * check-command: sparse -Wno-context $file
  *
  * check-error-start
+context-stmt.c:11:21: error: an expression is expected before ')'
+context-stmt.c:11:21: error: Expected ) in expression
+context-stmt.c:11:21: error: got ;
 context-stmt.c:7:21: error: bad constant expression
 context-stmt.c:8:23: error: bad constant expression
+context-stmt.c:11:20: error: bad constant expression type
  * check-error-end
  */
-- 
2.17.0

--
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