From: Nathan Sidwell Date: Fri, 16 Oct 2020 16:22:22 +0000 (-0700) Subject: c++: Fix null deref at EOF [PR96258] X-Git-Tag: upstream/12.2.0~12940 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a16da48bf19bb139e5461e5b5b7f072d5369b054;p=platform%2Fupstream%2Fgcc.git c++: Fix null deref at EOF [PR96258] cp_parser_declaration peeks at 1 or 2 tokens, when I changed it not to peek past EOF, I set the second token to NULL. But there are paths through the function that just look at the second token. Fixed by setting that token to EOF rather than NULL in this case. PR c++/96258 gcc/cp/ * parser.c (cp_parser_declaration): Make token2 point to EOF if token1 was EOF. gcc/testsuite/ * g++.dg/parse/pr96258.C: New. --- diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 592ce95..7ec7d42 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -13497,10 +13497,8 @@ cp_parser_declaration (cp_parser* parser) /* Try to figure out what kind of declaration is present. */ cp_token *token1 = cp_lexer_peek_token (parser->lexer); - cp_token *token2 = NULL; - - if (token1->type != CPP_EOF) - token2 = cp_lexer_peek_nth_token (parser->lexer, 2); + cp_token *token2 = (token1->type == CPP_EOF + ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2)); /* Get the high-water mark for the DECLARATOR_OBSTACK. */ void *p = obstack_alloc (&declarator_obstack, 0); diff --git a/gcc/testsuite/g++.dg/parse/pr96258.C b/gcc/testsuite/g++.dg/parse/pr96258.C new file mode 100644 index 0000000..1b642e1 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/pr96258.C @@ -0,0 +1,5 @@ +// { dg-additional-options -fopenmp } +// { dg-require-effective-target fopenmp } +#pragma omp declare simd // { dg-error "not immediately followed by" } + +// { dg-error "-:expected unqualified-id" "" { target *-*-* } .+1 }