[PATCH 3/6] context: __context__(...) expect a constant expression

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

 



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@xxxxxxxxx>
---
 linearize.c               |  6 +-----
 validation/context-stmt.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 5 deletions(-)
 create mode 100644 validation/context-stmt.c

diff --git a/linearize.c b/linearize.c
index 88f6c0949..6284d079b 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 000000000..cb85e562b
--- /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
+ */
-- 
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