Linux-Sparse Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH 0/5] Fixes for generic selection
@ 2020-06-19 15:02 Luc Van Oostenryck
  2020-06-19 15:02 ` [PATCH 1/5] gensel: add testcases from DR481 Luc Van Oostenryck
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Luc Van Oostenryck @ 2020-06-19 15:02 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

This series contains:
* the controlling expression must be lvalue-converted,
  array-to-pointer & function-to-pointer converted.
  These are the rules following the resolution of DR481
  and are also used for C17 (and are followed by both
  GCC & clang).
* validate the type of the associations.

These patches are available for testing & review at:
  git://git.kernel.org/pub/scm/devel/sparse/sparse.git fix-gensel


Note: A small inconsistency remains: a plain char
      will match a signed char or a unsigned char.

Luc Van Oostenryck (5):
  gensel: add testcases from DR481
  gensel: use temporary variable in generic selection
  gensel: controlling expression must be lvalue converted
  gensel: controlling expression must be pointer-converted
  gensel: validate the type of the associations

 evaluate.c                 | 33 ++++++++++++++++++++++++++++++---
 validation/generic-dr481.c | 17 +++++++++++++++++
 2 files changed, 47 insertions(+), 3 deletions(-)
 create mode 100644 validation/generic-dr481.c

-- 
2.27.0

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

* [PATCH 1/5] gensel: add testcases from DR481
  2020-06-19 15:02 [PATCH 0/5] Fixes for generic selection Luc Van Oostenryck
@ 2020-06-19 15:02 ` Luc Van Oostenryck
  2020-06-19 15:02 ` [PATCH 2/5] gensel: use temporary variable in generic selection Luc Van Oostenryck
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Luc Van Oostenryck @ 2020-06-19 15:02 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

Following the resolution of DR481, the controlling expression
is subject to a few different rules.

Add the testcases from this defect report.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 validation/generic-dr481.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
 create mode 100644 validation/generic-dr481.c

diff --git a/validation/generic-dr481.c b/validation/generic-dr481.c
new file mode 100644
index 000000000000..2ed15c9bc6d3
--- /dev/null
+++ b/validation/generic-dr481.c
@@ -0,0 +1,17 @@
+static char const* a = _Generic("bla", char*: "blu");
+static char const* b = _Generic("bla", char[4]: "blu");
+static char const* c = _Generic((int const){ 0 }, int: "blu");
+static char const* d = _Generic((int const){ 0 }, int const: "blu");
+static char const* e = _Generic(+(int const){ 0 }, int: "blu");
+static char const* f = _Generic(+(int const){ 0 }, int const: "blu");
+
+/*
+ * check-name: generic-dr481
+ * check-known-to-fail
+ *
+ * check-error-start
+generic-dr481.c:2:32: error: no generic selection for 'char *'
+generic-dr481.c:4:32: error: no generic selection for 'int const [toplevel]'
+generic-dr481.c:6:32: error: no generic selection for 'int'
+ * check-error-end
+ */
-- 
2.27.0

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

* [PATCH 2/5] gensel: use temporary variable in generic selection
  2020-06-19 15:02 [PATCH 0/5] Fixes for generic selection Luc Van Oostenryck
  2020-06-19 15:02 ` [PATCH 1/5] gensel: add testcases from DR481 Luc Van Oostenryck
@ 2020-06-19 15:02 ` Luc Van Oostenryck
  2020-06-19 15:02 ` [PATCH 3/5] gensel: controlling expression must be lvalue converted Luc Van Oostenryck
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Luc Van Oostenryck @ 2020-06-19 15:02 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

Use a temporary variable for 'map->type' to make the expressions
in the following patches more readable.

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

diff --git a/evaluate.c b/evaluate.c
index 95aef4dc0758..cf40eed418b4 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -3293,9 +3293,11 @@ static struct symbol *evaluate_generic_selection(struct expression *expr)
 		return NULL;
 
 	for (map = expr->map; map; map = map->next) {
-		if (!evaluate_symbol(map->type))
+		struct symbol *stype = map->type;
+
+		if (!evaluate_symbol(stype))
 			continue;
-		if (!type_selection(ctrl, map->type))
+		if (!type_selection(ctrl, stype))
 			continue;
 
 		res = map->expr;
-- 
2.27.0

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

* [PATCH 3/5] gensel: controlling expression must be lvalue converted
  2020-06-19 15:02 [PATCH 0/5] Fixes for generic selection Luc Van Oostenryck
  2020-06-19 15:02 ` [PATCH 1/5] gensel: add testcases from DR481 Luc Van Oostenryck
  2020-06-19 15:02 ` [PATCH 2/5] gensel: use temporary variable in generic selection Luc Van Oostenryck
@ 2020-06-19 15:02 ` Luc Van Oostenryck
  2020-06-19 15:02 ` [PATCH 4/5] gensel: controlling expression must be pointer-converted Luc Van Oostenryck
  2020-06-19 15:03 ` [PATCH 5/5] gensel: validate the type of the associations Luc Van Oostenryck
  4 siblings, 0 replies; 6+ messages in thread
From: Luc Van Oostenryck @ 2020-06-19 15:02 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck, Marco Elver

Following the resolution of DR481, the controlling expression
of a generic selection must be lvalue converted. In other words,
the qualifiers must be ignored.

Reported-by: Marco Elver <elver@google.com>
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 evaluate.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/evaluate.c b/evaluate.c
index cf40eed418b4..d8615a894c0d 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -3287,17 +3287,20 @@ static struct symbol *evaluate_generic_selection(struct expression *expr)
 {
 	struct type_expression *map;
 	struct expression *res;
+	struct symbol source;
 	struct symbol *ctrl;
 
 	if (!(ctrl = evaluate_expression(expr->control)))
 		return NULL;
 
+	source = *ctrl;
+	source.ctype.modifiers &= ~(MOD_QUALIFIER|MOD_ATOMIC);
 	for (map = expr->map; map; map = map->next) {
 		struct symbol *stype = map->type;
 
 		if (!evaluate_symbol(stype))
 			continue;
-		if (!type_selection(ctrl, stype))
+		if (!type_selection(&source, stype))
 			continue;
 
 		res = map->expr;
-- 
2.27.0

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

* [PATCH 4/5] gensel: controlling expression must be pointer-converted
  2020-06-19 15:02 [PATCH 0/5] Fixes for generic selection Luc Van Oostenryck
                   ` (2 preceding siblings ...)
  2020-06-19 15:02 ` [PATCH 3/5] gensel: controlling expression must be lvalue converted Luc Van Oostenryck
@ 2020-06-19 15:02 ` Luc Van Oostenryck
  2020-06-19 15:03 ` [PATCH 5/5] gensel: validate the type of the associations Luc Van Oostenryck
  4 siblings, 0 replies; 6+ messages in thread
From: Luc Van Oostenryck @ 2020-06-19 15:02 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck, Marco Elver

Following the resolution of DR481, the controlling expression
of a generic selection must be array-to-pointer converted and
function-to-pointer converted.

Do this by adding a call to degenerate().

Reported-by: Marco Elver <elver@google.com>
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 evaluate.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/evaluate.c b/evaluate.c
index d8615a894c0d..491dfa3c6b89 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -3290,7 +3290,9 @@ static struct symbol *evaluate_generic_selection(struct expression *expr)
 	struct symbol source;
 	struct symbol *ctrl;
 
-	if (!(ctrl = evaluate_expression(expr->control)))
+	if (!evaluate_expression(expr->control))
+		return NULL;
+	if (!(ctrl = degenerate(expr->control)))
 		return NULL;
 
 	source = *ctrl;
-- 
2.27.0

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

* [PATCH 5/5] gensel: validate the type of the associations
  2020-06-19 15:02 [PATCH 0/5] Fixes for generic selection Luc Van Oostenryck
                   ` (3 preceding siblings ...)
  2020-06-19 15:02 ` [PATCH 4/5] gensel: controlling expression must be pointer-converted Luc Van Oostenryck
@ 2020-06-19 15:03 ` Luc Van Oostenryck
  4 siblings, 0 replies; 6+ messages in thread
From: Luc Van Oostenryck @ 2020-06-19 15:03 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

The type in a generic association must correspond to a complete
type and not a variably modified type.

Add validation for this.

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

diff --git a/evaluate.c b/evaluate.c
index 491dfa3c6b89..aa0f208006bb 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -3299,9 +3299,29 @@ static struct symbol *evaluate_generic_selection(struct expression *expr)
 	source.ctype.modifiers &= ~(MOD_QUALIFIER|MOD_ATOMIC);
 	for (map = expr->map; map; map = map->next) {
 		struct symbol *stype = map->type;
+		struct symbol *base;
 
 		if (!evaluate_symbol(stype))
 			continue;
+
+		if (stype->type == SYM_NODE)
+			base = stype->ctype.base_type;
+
+		if (base->type == SYM_ARRAY && base->array_size) {
+			get_expression_value_silent(base->array_size);
+			if (base->array_size->type == EXPR_VALUE)
+				continue;
+			sparse_error(stype->pos, "variable length array type in generic selection");
+			continue;
+		}
+		if (is_func_type(stype)) {
+			sparse_error(stype->pos, "function type in generic selection");
+			continue;
+		}
+		if (stype->bit_size <= 0 || is_void_type(stype)) {
+			sparse_error(stype->pos, "incomplete type in generic selection");
+			continue;
+		}
 		if (!type_selection(&source, stype))
 			continue;
 
-- 
2.27.0

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

end of thread, back to index

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-19 15:02 [PATCH 0/5] Fixes for generic selection Luc Van Oostenryck
2020-06-19 15:02 ` [PATCH 1/5] gensel: add testcases from DR481 Luc Van Oostenryck
2020-06-19 15:02 ` [PATCH 2/5] gensel: use temporary variable in generic selection Luc Van Oostenryck
2020-06-19 15:02 ` [PATCH 3/5] gensel: controlling expression must be lvalue converted Luc Van Oostenryck
2020-06-19 15:02 ` [PATCH 4/5] gensel: controlling expression must be pointer-converted Luc Van Oostenryck
2020-06-19 15:03 ` [PATCH 5/5] gensel: validate the type of the associations Luc Van Oostenryck

Linux-Sparse Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-sparse/0 linux-sparse/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-sparse linux-sparse/ https://lore.kernel.org/linux-sparse \
		linux-sparse@vger.kernel.org
	public-inbox-index linux-sparse

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-sparse


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git