Skip to content

Commit 0b390be

Browse files
authored
fixed #13719 - fixed nullptr dereference in Library::detectSmartPointer() (#7379)
1 parent e0d6145 commit 0b390be

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

lib/library.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,8 @@ bool Library::isSmartPointer(const Token* tok) const
18921892

18931893
const Library::SmartPointer* Library::detectSmartPointer(const Token* tok, bool withoutStd) const
18941894
{
1895+
if (!tok)
1896+
return nullptr;
18951897
std::string typestr = withoutStd ? "std::" : "";
18961898
if (tok->str() == "::")
18971899
tok = tok->next();

test/testlibrary.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class TestLibrary : public TestFixture {
6969
TEST_CASE(version);
7070
TEST_CASE(loadLibErrors);
7171
TEST_CASE(loadLibCombinations);
72+
TEST_CASE(smartpointer);
7273
}
7374

7475
void isCompliantValidationExpression() const {
@@ -868,6 +869,8 @@ class TestLibrary : public TestFixture {
868869
ASSERT_EQUALS(F.endPattern, "");
869870
ASSERT_EQUALS(F.itEndPattern, ":: iterator");
870871

872+
ASSERT(!library.detectContainerOrIterator(nullptr));
873+
871874
{
872875
const SimpleTokenizer var(*this, "std::A<int> a;");
873876
ASSERT_EQUALS(&A, library.detectContainer(var.tokens()));
@@ -1140,6 +1143,15 @@ class TestLibrary : public TestFixture {
11401143
ASSERT_EQUALS(s.library.defines().empty(), false);
11411144
}
11421145
}
1146+
1147+
void smartpointer() const {
1148+
const Settings s = settingsBuilder().library("std.cfg").build();
1149+
const Library& library = s.library;
1150+
1151+
ASSERT(!library.detectSmartPointer(nullptr));
1152+
1153+
// TODO: add more tests
1154+
}
11431155
};
11441156

11451157
REGISTER_TEST(TestLibrary)

test/testsymboldatabase.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,8 @@ class TestSymbolDatabase : public TestFixture {
612612
TEST_CASE(testValuetypeOriginalName);
613613

614614
TEST_CASE(dumpFriend); // Check if isFriend added to dump file
615+
616+
TEST_CASE(smartPointerLookupCtor); // #13719);
615617
}
616618

617619
void array() {
@@ -11190,6 +11192,14 @@ class TestSymbolDatabase : public TestFixture {
1119011192
db->printXml(ostr);
1119111193
ASSERT(ostr.str().find(" isFriend=\"true\"") != std::string::npos);
1119211194
}
11195+
11196+
void smartPointerLookupCtor() { // #13719
11197+
// do not crash in smartpointer lookup
11198+
GET_SYMBOL_DB("struct S { int v; S(int i); };\n"
11199+
"void f() { S(0).v; }");
11200+
11201+
ASSERT(db);
11202+
}
1119311203
};
1119411204

1119511205
REGISTER_TEST(TestSymbolDatabase)

0 commit comments

Comments
 (0)