Skip to content

Commit 389e446

Browse files
authored
Fix 11848: Assert failure in getParentValueTypes() (#5274)
1 parent 931a59a commit 389e446

3 files changed

Lines changed: 29 additions & 9 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3490,16 +3490,28 @@ const Token *Type::initBaseInfo(const Token *tok, const Token *tok1)
34903490
return tok2;
34913491
}
34923492

3493-
const std::string& Type::name() const
3493+
std::string Type::name() const
34943494
{
3495-
const Token* next = classDef->next();
3495+
const Token* start = classDef->next();
34963496
if (classScope && classScope->enumClass && isEnumType())
3497-
return next->strAt(1);
3498-
if (next->str() == "class")
3499-
return next->strAt(1);
3500-
if (next->isName())
3501-
return next->str();
3502-
return emptyString;
3497+
start = start->tokAt(1);
3498+
else if (start->str() == "class")
3499+
start = start->tokAt(1);
3500+
else if (!start->isName())
3501+
return emptyString;
3502+
const Token* next = start;
3503+
while (Token::Match(next, "::|<|>|(|)|[|]|*|&|&&|%name%")) {
3504+
if (Token::Match(next, "<|(|[") && next->link())
3505+
next = next->link();
3506+
next = next->next();
3507+
}
3508+
std::string result;
3509+
for (const Token* tok = start; tok != next; tok = tok->next()) {
3510+
if (!result.empty())
3511+
result += ' ';
3512+
result += tok->str();
3513+
}
3514+
return result;
35033515
}
35043516

35053517
void SymbolDatabase::debugMessage(const Token *tok, const std::string &type, const std::string &msg) const

lib/symboldatabase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class CPPCHECKLIB Type {
131131
}
132132
}
133133

134-
const std::string& name() const;
134+
std::string name() const;
135135

136136
const std::string& type() const {
137137
return classDef ? classDef->str() : emptyString;

test/testvalueflow.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6812,6 +6812,14 @@ class TestValueFlow : public TestFixture {
68126812
" dummy_resource::log.clear();\n"
68136813
"}\n";
68146814
valueOfTok(code, "log");
6815+
6816+
code = "struct D : B<int> {\n"
6817+
" D(int i, const std::string& s) : B<int>(i, s) {}\n"
6818+
"};\n"
6819+
"template<> struct B<int>::S {\n"
6820+
" int j;\n"
6821+
"};\n";
6822+
valueOfTok(code, "B");
68156823
}
68166824

68176825
void valueFlowCrash() {

0 commit comments

Comments
 (0)