aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-05-24 02:01:21 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-05-26 17:04:29 +0200
commitbf28f6e2b80f81dd5ebd6431209712e0287f0587 (patch)
tree8dc56bf71d6513777fa8f70a422ae5523fd349df
parent02510ae80084b9446d9afda779c0c4a3385eb22d (diff)
downloadsparse-dev-bf28f6e2b80f81dd5ebd6431209712e0287f0587.tar.gz
context: fix crashes while parsing '__context__;' or '__context__(;'
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@gmail.com>
-rw-r--r--parse.c4
-rw-r--r--validation/context-stmt.c7
2 files changed, 10 insertions, 1 deletions
diff --git a/parse.c b/parse.c
index 68cdeb22..42b3fd20 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 cb85e562..1f02c3a6 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
*/