diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 35ca24485..5a8e23011 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -206,7 +206,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti } // using namespace - else if (Token::Match(tok, "using namespace ::| %type% ;|::")) { + else if (_tokenizer->isCPP() && Token::Match(tok, "using namespace ::| %type% ;|::")) { Scope::UsingInfo using_info; using_info.start = tok; // save location @@ -622,7 +622,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti } // friend class declaration? - else if (Token::Match(tok, "friend class| ::| %any% ;|::")) { + else if (_tokenizer->isCPP() && Token::Match(tok, "friend class| ::| %any% ;|::")) { Type::FriendInfo friendInfo; // save the name start @@ -644,6 +644,10 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti // fill this in after parsing is complete friendInfo.type = 0; + if (!scope->definedType) { + _tokenizer->syntaxError(tok); + return; + } scope->definedType->friendList.push_back(friendInfo); } } else if (scope->type == Scope::eNamespace || scope->type == Scope::eGlobal) { diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 32032197a..bd7bf9cd0 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -121,6 +121,7 @@ private: TEST_CASE(garbageCode80); TEST_CASE(garbageCode81); TEST_CASE(garbageCode82); + TEST_CASE(garbageCode83); TEST_CASE(garbageValueFlow); TEST_CASE(garbageSymbolDatabase); @@ -671,6 +672,10 @@ private: ASSERT_THROW(checkCode("p(\"Hello \" 14) _yn(const size_t) typedef bool pfunk (*pfunk)(const size_t)"), InternalError); } + void garbageCode83() { // #6771 + ASSERT_THROW(checkCode("namespace A { class } class A { friend C ; } { } ;"), InternalError); + } + void garbageValueFlow() { // #6089 const char* code = "{} int foo(struct, x1, struct x2, x3, int, x5, x6, x7)\n"