Skip to content

Commit cde1b8d

Browse files
Fix #14098 FN nullPointer constructing std::string from known function result (regression) (danmar#8141)
1 parent eef2137 commit cde1b8d

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lib/checknullpointer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown, const Set
223223
return true;
224224

225225
// std::string dereferences nullpointers
226-
if (Token::Match(parent->tokAt(-3), "std :: string|wstring (|{ %name% )|}"))
226+
if (Token::Match(parent->tokAt(-3), "std :: string|wstring (|{"))
227227
return true;
228-
if (Token::Match(parent->previous(), "%name% (|{ %name% )|}")) {
229-
const Variable* var = tok->tokAt(-2)->variable();
228+
if (Token::Match(parent->previous(), "%name% (|{")) {
229+
const Variable* var = parent->previous()->variable();
230230
if (var && !var->isPointer() && !var->isArray() && var->isStlStringType())
231231
return true;
232232
}

test/testnullpointer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3944,6 +3944,15 @@ class TestNullPointer : public TestFixture {
39443944
"void f() { std::string s = g(0L); }\n");
39453945
ASSERT_EQUALS("[test.cpp:2:29]: (error) Null pointer dereference: g(0L) [nullPointer]\n",
39463946
errout_str());
3947+
3948+
check("const char* g() { return nullptr; }\n" // #14098
3949+
"std::string f() {\n"
3950+
" std::string s{ g() };\n"
3951+
" return s + std::string(g());\n"
3952+
"}\n");
3953+
ASSERT_EQUALS("[test.cpp:3:21]: (error) Null pointer dereference: g() [nullPointer]\n"
3954+
"[test.cpp:4:29]: (error) Null pointer dereference: g() [nullPointer]\n",
3955+
errout_str());
39473956
}
39483957

39493958
void nullpointerStdStream() {

0 commit comments

Comments
 (0)