From bbd2e88cdd9d36d47ce50204d18547e08f2e2bea Mon Sep 17 00:00:00 2001 From: Martin Nagy Date: Mon, 27 Apr 2009 10:48:50 +0200 Subject: [PATCH] Print an error if typeof() lacks an argument We weren't checking if the initializer isn't NULL, which caused sparse to segfault later on when performing lazy evaluation in classify_type(). Signed-off-by: Martin Nagy --- parse.c | 17 +++++++++++------ validation/bad-typeof.c | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 validation/bad-typeof.c diff --git a/parse.c b/parse.c index 9662122..604e528 100644 --- a/parse.c +++ b/parse.c @@ -924,12 +924,17 @@ static struct token *typeof_specifier(struct token *token, struct decl_state *ct ctx->ctype.base_type = sym->ctype.base_type; apply_ctype(token->pos, &sym->ctype, &ctx->ctype); } else { - struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF); - token = parse_expression(token->next, &typeof_sym->initializer); - - typeof_sym->endpos = token->pos; - ctx->ctype.base_type = typeof_sym; - } + struct expression *expr = NULL; + token = parse_expression(token->next, &expr); + if (expr) { + struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF); + typeof_sym->endpos = token->pos; + typeof_sym->initializer = expr; + ctx->ctype.base_type = typeof_sym; + } else { + sparse_error(token->pos, "expected expression after the '(' token"); + } + } return expect(token, ')', "after typeof"); } diff --git a/validation/bad-typeof.c b/validation/bad-typeof.c new file mode 100644 index 0000000..5c27de4 --- /dev/null +++ b/validation/bad-typeof.c @@ -0,0 +1,15 @@ +static int fun(void) +{ + typeof() a; + int b; + + a = b; +} +/* + * check-name: Bad typeof syntax segfault + * + * check-error-start +bad-typeof.c:3:16: error: expected expression after the '(' token +bad-typeof.c:6:9: error: identifier 'a' has no type + * check-error-end + */ -- 1.6.0.6