diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 796f61470..9ac0a9d80 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2516,7 +2516,7 @@ void Tokenizer::setVarId() } } else if (tok->str() == "}") { // parse anonymous unions as part of the current scope - if (!(Token::simpleMatch(tok, "} ;") && Token::simpleMatch(tok->link()->previous(), "union {"))) { + if (!(Token::simpleMatch(tok, "} ;") && tok->link() && Token::simpleMatch(tok->link()->previous(), "union {"))) { // Set variable ids in class declaration.. if (!isC() && !executableScope.top() && tok->link()) { setVarIdClassDeclaration(tok->link(), diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index e266792c8..4bfebd9f1 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -67,6 +67,7 @@ private: TEST_CASE(tokenize30); // #5356 (segmentation fault upon invalid code) TEST_CASE(tokenize31); // #3503 (Wrong handling of member function taking function pointer as argument) TEST_CASE(tokenize32); // #5884 (fsanitize=undefined: left shift of negative value -10000 in lib/templatesimplifier.cpp:852:46) + TEST_CASE(tokenize33); // #5780 Various crashes on valid template code // don't freak out when the syntax is wrong TEST_CASE(wrong_syntax1); @@ -905,6 +906,15 @@ private: ASSERT_EQUALS(code, tokenizeAndStringify(code)); } + // ##5780 Various crashes on valid template code in Tokenizer::setVarId() + void tokenize33() { + const char * code = "template> struct vector {};\n" + "void z() {\n" + " vector VI;\n" + "}\n"; + ASSERT_EQUALS(code, tokenizeAndStringify(code)); + } + void wrong_syntax1() { { const char code[] ="TR(kvmpio, PROTO(int rw), ARGS(rw), TP_(aa->rw;))";