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
commit02510ae80084b9446d9afda779c0c4a3385eb22d (patch)
tree3479d8f1060d550364038379c75458db5f617605
parent8e9e4da4790a071c6fa0dd5bf99036c36b5d5fbd (diff)
downloadsparse-dev-02510ae80084b9446d9afda779c0c4a3385eb22d.tar.gz
context: __context__(...) expect a constant expression
No check are done if the inc/dec value of context statements is effectively a compile-time integer value: '0' is silently used if it is not. Change that by using get_expression_value() when linearizing context statements (which has the added advantage to also slightly simplify the code). Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--linearize.c6
-rw-r--r--validation/context-stmt.c19
2 files changed, 20 insertions, 5 deletions
diff --git a/linearize.c b/linearize.c
index 88f6c094..6284d079 100644
--- a/linearize.c
+++ b/linearize.c
@@ -1777,12 +1777,8 @@ static pseudo_t linearize_context(struct entrypoint *ep, struct statement *stmt)
{
struct instruction *insn = alloc_instruction(OP_CONTEXT, 0);
struct expression *expr = stmt->expression;
- int value = 0;
- if (expr->type == EXPR_VALUE)
- value = expr->value;
-
- insn->increment = value;
+ insn->increment = get_expression_value(expr);
insn->context_expr = stmt->context;
add_one_insn(ep, insn);
return VOID;
diff --git a/validation/context-stmt.c b/validation/context-stmt.c
new file mode 100644
index 00000000..cb85e562
--- /dev/null
+++ b/validation/context-stmt.c
@@ -0,0 +1,19 @@
+static void foo(int x)
+{
+ __context__(0); // OK
+ __context__(x, 0); // OK
+ __context__ (x, 1); // OK
+
+ __context__(x); // KO: no const expr
+ __context__(1,x); // KO: no const expr
+}
+
+/*
+ * check-name: context-stmt
+ * check-command: sparse -Wno-context $file
+ *
+ * check-error-start
+context-stmt.c:7:21: error: bad constant expression
+context-stmt.c:8:23: error: bad constant expression
+ * check-error-end
+ */