From ad9670d0530c31d171103ac3ea60c8aba20f9a80 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Fri, 19 Jun 2020 15:02:19 +0200 Subject: gensel: validate the type of the associations 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 --- evaluate.c | 20 ++++++++++++++++++++ validation/generic-bad0.c | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/evaluate.c b/evaluate.c index 491dfa3c..aa0f2080 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; diff --git a/validation/generic-bad0.c b/validation/generic-bad0.c index acc3d5e7..d11030db 100644 --- a/validation/generic-bad0.c +++ b/validation/generic-bad0.c @@ -18,6 +18,10 @@ void foo(int n) generic-bad0.c:5:33: warning: multiple default in generic expression generic-bad0.c:5:30: note: previous was here generic-bad0.c:6:25: warning: Variable length array is used. +generic-bad0.c:6:21: error: variable length array type in generic selection +generic-bad0.c:7:21: error: incomplete type in generic selection +generic-bad0.c:8:21: error: incomplete type in generic selection +generic-bad0.c:9:21: error: function type in generic selection generic-bad0.c:11:17: error: no generic selection for 'int [addressable] n' * check-error-end */ -- cgit 1.2.3-korg