Skip to content

Commit 8b6cbe2

Browse files
Fix crash in SymbolDatabase::addClassFunction() (f'up to #12209) (#5689)
1 parent 2b61c9e commit 8b6cbe2

2 files changed

Lines changed: 42 additions & 22 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3286,7 +3286,7 @@ void SymbolDatabase::addClassFunction(Scope **scope, const Token **tok, const To
32863286
tok1 = tok1->tokAt(2);
32873287
scope2 = scope2->findRecordInNestedList(tok1->str());
32883288
}
3289-
if (isAnonymousNamespace)
3289+
if (scope2 && isAnonymousNamespace)
32903290
scope2 = scope2->findRecordInNestedList(tok1->str());
32913291

32923292
if (count == 1 && scope2) {

test/testsymboldatabase.cpp

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5464,27 +5464,47 @@ class TestSymbolDatabase : public TestFixture {
54645464

54655465
void createSymbolDatabaseFindAllScopes7()
54665466
{
5467-
GET_SYMBOL_DB("namespace {\n"
5468-
" struct S {\n"
5469-
" void f();\n"
5470-
" };\n"
5471-
"}\n"
5472-
"void S::f() {}\n");
5473-
ASSERT(db);
5474-
ASSERT_EQUALS(4, db->scopeList.size());
5475-
auto anon = db->scopeList.begin();
5476-
++anon;
5477-
ASSERT(anon->className.empty());
5478-
ASSERT_EQUALS(anon->type, Scope::eNamespace);
5479-
auto S = anon;
5480-
++S;
5481-
ASSERT_EQUALS(S->type, Scope::eStruct);
5482-
ASSERT_EQUALS(S->className, "S");
5483-
ASSERT_EQUALS(S->nestedIn, &*anon);
5484-
const Token* f = Token::findsimplematch(tokenizer.tokens(), "f ( ) {");
5485-
ASSERT(f && f->function() && f->function()->functionScope && f->function()->functionScope->bodyStart);
5486-
ASSERT_EQUALS(f->function()->functionScope->functionOf, &*S);
5487-
ASSERT_EQUALS(f->function()->functionScope->bodyStart->linenr(), 6);
5467+
{
5468+
GET_SYMBOL_DB("namespace {\n"
5469+
" struct S {\n"
5470+
" void f();\n"
5471+
" };\n"
5472+
"}\n"
5473+
"void S::f() {}\n");
5474+
ASSERT(db);
5475+
ASSERT_EQUALS(4, db->scopeList.size());
5476+
auto anon = db->scopeList.begin();
5477+
++anon;
5478+
ASSERT(anon->className.empty());
5479+
ASSERT_EQUALS(anon->type, Scope::eNamespace);
5480+
auto S = anon;
5481+
++S;
5482+
ASSERT_EQUALS(S->type, Scope::eStruct);
5483+
ASSERT_EQUALS(S->className, "S");
5484+
ASSERT_EQUALS(S->nestedIn, &*anon);
5485+
const Token* f = Token::findsimplematch(tokenizer.tokens(), "f ( ) {");
5486+
ASSERT(f && f->function() && f->function()->functionScope && f->function()->functionScope->bodyStart);
5487+
ASSERT_EQUALS(f->function()->functionScope->functionOf, &*S);
5488+
ASSERT_EQUALS(f->function()->functionScope->bodyStart->linenr(), 6);
5489+
}
5490+
{
5491+
GET_SYMBOL_DB("namespace {\n"
5492+
" int i = 0;\n"
5493+
"}\n"
5494+
"namespace N {\n"
5495+
" namespace {\n"
5496+
" template<typename T>\n"
5497+
" struct S {\n"
5498+
" void f();\n"
5499+
" };\n"
5500+
" template<typename T>\n"
5501+
" void S<T>::f() {}\n"
5502+
" }\n"
5503+
" S<int> g() { return {}; }\n"
5504+
"}\n");
5505+
ASSERT(db); // don't crash
5506+
ASSERT_EQUALS("", errout.str());
5507+
}
54885508
}
54895509

54905510
void createSymbolDatabaseIncompleteVars()

0 commit comments

Comments
 (0)