From d1a006c8b9421a33ebf262fe1e5537ae5a41e605 Mon Sep 17 00:00:00 2001 From: Michael Beck Date: Wed, 12 Dec 2007 15:05:44 +0000 Subject: [PATCH] fixed crash with unclosed string literal [r18710] --- parser.c | 13 +++++++++++-- parsetest/shouldfail/unclosed_string.c | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 parsetest/shouldfail/unclosed_string.c diff --git a/parser.c b/parser.c index fdc2839..1550ee7 100644 --- a/parser.c +++ b/parser.c @@ -1264,6 +1264,10 @@ static initializer_t *parse_initializer(type_t *type) if(token.type != '{') { expression_t *expression = parse_assignment_expression(); + if (expression->base.datatype == NULL) { + /* something bad happens, don't produce further errors */ + return NULL; + } initializer_t *initializer = initializer_from_expression(type, expression); if(initializer == NULL) { errorf(HERE, @@ -2929,10 +2933,15 @@ static expression_t *create_invalid_expression(void) return expression; } +/** + * Prints an error message if an expression was expected but not read + */ static expression_t *expected_expression_error(void) { - errorf(HERE, "expected expression, got token '%K'", &token); - + /* skip the error message if the error token was read */ + if (token.type != T_ERROR) { + errorf(HERE, "expected expression, got token '%K'", &token); + } next_token(); return create_invalid_expression(); diff --git a/parsetest/shouldfail/unclosed_string.c b/parsetest/shouldfail/unclosed_string.c new file mode 100644 index 0000000..9ccb169 --- /dev/null +++ b/parsetest/shouldfail/unclosed_string.c @@ -0,0 +1 @@ +char s[] = "unclosed string -- 2.20.1