linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/5] fix qualifier dropping
@ 2020-11-18 21:11 Luc Van Oostenryck
  2020-11-18 21:11 ` [PATCH 1/5] unqual: add testcases Luc Van Oostenryck
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Luc Van Oostenryck @ 2020-11-18 21:11 UTC (permalink / raw)
  To: linux-sparse; +Cc: Linus Torvalds, Luc Van Oostenryck

This series fixes missing qualifier dropping in comma and
statement expressions and superfluous qualifier dropping in
increment or decrement expressions.

Luc Van Oostenryck (5):
  unqual: add testcases
  unqual: unqualify_type() should check for null ctypes
  unqual: comma expressions should drop qualifiers
  unqual: statement expressions should drop qualifiers
  unqual: pre- & post-increment/decrement should *not* drop qualifiers

 evaluate.c                         |  9 +++++----
 validation/eval/unqual-comma.c     | 12 ++++++++++++
 validation/eval/unqual-postop.c    | 22 ++++++++++++++++++++++
 validation/eval/unqual-stmt-expr.c | 12 ++++++++++++
 validation/eval/unqual02.c         | 26 ++++++++++++++++++++++++++
 5 files changed, 77 insertions(+), 4 deletions(-)
 create mode 100644 validation/eval/unqual-comma.c
 create mode 100644 validation/eval/unqual-postop.c
 create mode 100644 validation/eval/unqual-stmt-expr.c
 create mode 100644 validation/eval/unqual02.c

-- 
2.29.2


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

* [PATCH 1/5] unqual: add testcases
  2020-11-18 21:11 [PATCH 0/5] fix qualifier dropping Luc Van Oostenryck
@ 2020-11-18 21:11 ` Luc Van Oostenryck
  2020-11-18 21:11 ` [PATCH 2/5] unqual: unqualify_type() should check for null ctypes Luc Van Oostenryck
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Luc Van Oostenryck @ 2020-11-18 21:11 UTC (permalink / raw)
  To: linux-sparse; +Cc: Linus Torvalds, Luc Van Oostenryck

Add some testcases related to qualifier dropping / lvalue conversion.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 validation/eval/unqual-comma.c     | 13 +++++++++++++
 validation/eval/unqual-postop.c    | 23 +++++++++++++++++++++++
 validation/eval/unqual-stmt-expr.c | 13 +++++++++++++
 validation/eval/unqual02.c         | 26 ++++++++++++++++++++++++++
 4 files changed, 75 insertions(+)
 create mode 100644 validation/eval/unqual-comma.c
 create mode 100644 validation/eval/unqual-postop.c
 create mode 100644 validation/eval/unqual-stmt-expr.c
 create mode 100644 validation/eval/unqual02.c

diff --git a/validation/eval/unqual-comma.c b/validation/eval/unqual-comma.c
new file mode 100644
index 000000000000..e06586cd43e3
--- /dev/null
+++ b/validation/eval/unqual-comma.c
@@ -0,0 +1,13 @@
+#define __unqual_typeof(x) typeof(((void)0, (x)))
+
+int *foo(volatile int x);
+int *foo(volatile int x)
+{
+	extern __unqual_typeof(x) y;
+	return &y;
+}
+
+/*
+ * check-name: unqual-comma
+ * check-known-to-fail
+ */
diff --git a/validation/eval/unqual-postop.c b/validation/eval/unqual-postop.c
new file mode 100644
index 000000000000..fb3082dc8836
--- /dev/null
+++ b/validation/eval/unqual-postop.c
@@ -0,0 +1,23 @@
+static void test_volatile(void)
+{
+	volatile int x = 0;
+	int *pp;
+
+	typeof(++x)		v1; pp = &v1;	// KO
+	typeof(x++)		v2; pp = &v2;	// KO
+}
+
+/*
+ * check-name: unqual-postop
+ * check-command: sparse -Wno-declaration-after-statement $file
+ * check-known-to-fail
+ *
+ * check-error-start
+eval/unqual-postop.c:6:40: warning: incorrect type in assignment (different modifiers)
+eval/unqual-postop.c:6:40:    expected int *pp
+eval/unqual-postop.c:6:40:    got int volatile *
+eval/unqual-postop.c:7:40: warning: incorrect type in assignment (different modifiers)
+eval/unqual-postop.c:7:40:    expected int *pp
+eval/unqual-postop.c:7:40:    got int volatile *
+ * check-error-end
+ */
diff --git a/validation/eval/unqual-stmt-expr.c b/validation/eval/unqual-stmt-expr.c
new file mode 100644
index 000000000000..bac6cb6b197f
--- /dev/null
+++ b/validation/eval/unqual-stmt-expr.c
@@ -0,0 +1,13 @@
+#define __unqual_typeof(x) typeof(({ x; }))
+
+int *foo(volatile int x);
+int *foo(volatile int x)
+{
+	extern __unqual_typeof(x) y;
+	return &y;
+}
+
+/*
+ * check-name: unqual-stmt-expr
+ * check-known-to-fail
+ */
diff --git a/validation/eval/unqual02.c b/validation/eval/unqual02.c
new file mode 100644
index 000000000000..f136cbd50510
--- /dev/null
+++ b/validation/eval/unqual02.c
@@ -0,0 +1,26 @@
+static void test_const(volatile int x)
+{
+	const int x = 0;
+	typeof(1?x:x)		i3; i3 = 0;	// should be OK
+	typeof(+x)		i4; i4 = 0;	// should be OK
+	typeof(-x)		i5; i5 = 0;	// should be OK
+	typeof(!x)		i6; i6 = 0;	// should be OK
+	typeof(x+x)		i7; i7 = 0;	// should be OK
+}
+
+static void test_volatile(void)
+{
+	volatile int x = 0;
+	int *pp;
+
+	typeof(1?x:x)		i3; pp = &i3;	// should be OK
+	typeof(+x)		i4; pp = &i4;	// should be OK
+	typeof(-x)		i5; pp = &i5;	// should be OK
+	typeof(!x)		i6; pp = &i6;	// should be OK
+	typeof(x+x)		i7; pp = &i7;	// should be OK
+}
+
+/*
+ * check-name: unqual02
+ * check-command: sparse -Wno-declaration-after-statement $file
+ */
-- 
2.29.2


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

* [PATCH 2/5] unqual: unqualify_type() should check for null ctypes
  2020-11-18 21:11 [PATCH 0/5] fix qualifier dropping Luc Van Oostenryck
  2020-11-18 21:11 ` [PATCH 1/5] unqual: add testcases Luc Van Oostenryck
@ 2020-11-18 21:11 ` Luc Van Oostenryck
  2020-11-18 21:11 ` [PATCH 3/5] unqual: comma expressions should drop qualifiers Luc Van Oostenryck
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Luc Van Oostenryck @ 2020-11-18 21:11 UTC (permalink / raw)
  To: linux-sparse; +Cc: Linus Torvalds, Luc Van Oostenryck

It's possible that the input type is NULL, so add a check for it.

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

diff --git a/evaluate.c b/evaluate.c
index c39f9ec73da9..fd84205c7f2c 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -63,6 +63,8 @@ static inline int valid_subexpr_type(struct expression *expr)
 
 static struct symbol *unqualify_type(struct symbol *ctype)
 {
+	if (!ctype)
+		return ctype;
 	if (ctype->type == SYM_NODE && (ctype->ctype.modifiers & MOD_QUALIFIER)) {
 		struct symbol *unqual = alloc_symbol(ctype->pos, 0);
 
-- 
2.29.2


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

* [PATCH 3/5] unqual: comma expressions should drop qualifiers
  2020-11-18 21:11 [PATCH 0/5] fix qualifier dropping Luc Van Oostenryck
  2020-11-18 21:11 ` [PATCH 1/5] unqual: add testcases Luc Van Oostenryck
  2020-11-18 21:11 ` [PATCH 2/5] unqual: unqualify_type() should check for null ctypes Luc Van Oostenryck
@ 2020-11-18 21:11 ` Luc Van Oostenryck
  2020-11-18 21:11 ` [PATCH 4/5] unqual: statement " Luc Van Oostenryck
  2020-11-18 21:11 ` [PATCH 5/5] unqual: pre- & post-increment/decrement should *not* " Luc Van Oostenryck
  4 siblings, 0 replies; 6+ messages in thread
From: Luc Van Oostenryck @ 2020-11-18 21:11 UTC (permalink / raw)
  To: linux-sparse; +Cc: Linus Torvalds, Luc Van Oostenryck

Comma expressions should be subjected to lvalue-conversion
and thus should drop qualifiers.

Fix this by calling unqualify_type() after array-to-pointer
conversion.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 evaluate.c                     | 2 +-
 validation/eval/unqual-comma.c | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/evaluate.c b/evaluate.c
index fd84205c7f2c..b6e8477185f4 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -1028,7 +1028,7 @@ static struct symbol *evaluate_binop(struct expression *expr)
 
 static struct symbol *evaluate_comma(struct expression *expr)
 {
-	expr->ctype = degenerate(expr->right);
+	expr->ctype = unqualify_type(degenerate(expr->right));
 	if (expr->ctype == &null_ctype)
 		expr->ctype = &ptr_ctype;
 	expr->flags &= expr->left->flags & expr->right->flags;
diff --git a/validation/eval/unqual-comma.c b/validation/eval/unqual-comma.c
index e06586cd43e3..11546d22348a 100644
--- a/validation/eval/unqual-comma.c
+++ b/validation/eval/unqual-comma.c
@@ -9,5 +9,4 @@ int *foo(volatile int x)
 
 /*
  * check-name: unqual-comma
- * check-known-to-fail
  */
-- 
2.29.2


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

* [PATCH 4/5] unqual: statement expressions should drop qualifiers
  2020-11-18 21:11 [PATCH 0/5] fix qualifier dropping Luc Van Oostenryck
                   ` (2 preceding siblings ...)
  2020-11-18 21:11 ` [PATCH 3/5] unqual: comma expressions should drop qualifiers Luc Van Oostenryck
@ 2020-11-18 21:11 ` Luc Van Oostenryck
  2020-11-18 21:11 ` [PATCH 5/5] unqual: pre- & post-increment/decrement should *not* " Luc Van Oostenryck
  4 siblings, 0 replies; 6+ messages in thread
From: Luc Van Oostenryck @ 2020-11-18 21:11 UTC (permalink / raw)
  To: linux-sparse; +Cc: Linus Torvalds, Luc Van Oostenryck

Statement expressions should be subjected to lvalue-conversion
and thus should drop qualifiers.

Fix this by calling unqualify_type() after array-to-pointer
conversion.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 evaluate.c                         | 2 +-
 validation/eval/unqual-stmt-expr.c | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/evaluate.c b/evaluate.c
index b6e8477185f4..48ce61f0302d 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -3950,7 +3950,7 @@ struct symbol *evaluate_statement(struct statement *stmt)
 			return NULL;
 		if (stmt->expression->ctype == &null_ctype)
 			stmt->expression = cast_to(stmt->expression, &ptr_ctype);
-		return degenerate(stmt->expression);
+		return unqualify_type(degenerate(stmt->expression));
 
 	case STMT_COMPOUND: {
 		struct statement *s;
diff --git a/validation/eval/unqual-stmt-expr.c b/validation/eval/unqual-stmt-expr.c
index bac6cb6b197f..280c2fe8e36e 100644
--- a/validation/eval/unqual-stmt-expr.c
+++ b/validation/eval/unqual-stmt-expr.c
@@ -9,5 +9,4 @@ int *foo(volatile int x)
 
 /*
  * check-name: unqual-stmt-expr
- * check-known-to-fail
  */
-- 
2.29.2


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

* [PATCH 5/5] unqual: pre- & post-increment/decrement should *not* drop qualifiers
  2020-11-18 21:11 [PATCH 0/5] fix qualifier dropping Luc Van Oostenryck
                   ` (3 preceding siblings ...)
  2020-11-18 21:11 ` [PATCH 4/5] unqual: statement " Luc Van Oostenryck
@ 2020-11-18 21:11 ` Luc Van Oostenryck
  4 siblings, 0 replies; 6+ messages in thread
From: Luc Van Oostenryck @ 2020-11-18 21:11 UTC (permalink / raw)
  To: linux-sparse; +Cc: Linus Torvalds, Luc Van Oostenryck

Increment and decrement expressions are not subjected to lvalue-conversion
and thus should *not* drop qualifiers.

However, while the lvalue-conversion is not done, the qualifiers
are dropped because the type used for the result is the one returned
by classify_type() which always return the base type.

Fix this by using the type of the operand as the result type.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 evaluate.c                      | 3 +--
 validation/eval/unqual-postop.c | 1 -
 2 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/evaluate.c b/evaluate.c
index 48ce61f0302d..8599fcee6875 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -1935,8 +1935,7 @@ static struct symbol *evaluate_postop(struct expression *expr)
 	if (multiply) {
 		evaluate_assign_to(op, op->ctype);
 		expr->op_value = multiply;
-		expr->ctype = ctype;
-		return ctype;
+		return expr->ctype = op->ctype;
 	}
 
 	expression_error(expr, "bad argument type for ++/--");
diff --git a/validation/eval/unqual-postop.c b/validation/eval/unqual-postop.c
index fb3082dc8836..48b6be8b8508 100644
--- a/validation/eval/unqual-postop.c
+++ b/validation/eval/unqual-postop.c
@@ -10,7 +10,6 @@ static void test_volatile(void)
 /*
  * check-name: unqual-postop
  * check-command: sparse -Wno-declaration-after-statement $file
- * check-known-to-fail
  *
  * check-error-start
 eval/unqual-postop.c:6:40: warning: incorrect type in assignment (different modifiers)
-- 
2.29.2


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

end of thread, other threads:[~2020-11-18 21:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-11-18 21:11 [PATCH 0/5] fix qualifier dropping Luc Van Oostenryck
2020-11-18 21:11 ` [PATCH 1/5] unqual: add testcases Luc Van Oostenryck
2020-11-18 21:11 ` [PATCH 2/5] unqual: unqualify_type() should check for null ctypes Luc Van Oostenryck
2020-11-18 21:11 ` [PATCH 3/5] unqual: comma expressions should drop qualifiers Luc Van Oostenryck
2020-11-18 21:11 ` [PATCH 4/5] unqual: statement " Luc Van Oostenryck
2020-11-18 21:11 ` [PATCH 5/5] unqual: pre- & post-increment/decrement should *not* " 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).