Skip to content

Commit

Permalink
Fix 13410: False negative: invalidContainer when taking address of de…
Browse files Browse the repository at this point in the history
…referenced iterator (#7109)
  • Loading branch information
pfultz2 authored Dec 16, 2024
1 parent 1093382 commit 5be15e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,10 +1094,13 @@ static const ValueFlow::Value* getInnerLifetime(const Token* tok,
if (val.isInconclusive())
return nullptr;
if (val.capturetok)
return getInnerLifetime(val.capturetok, id, errorPath, depth - 1);
if (const ValueFlow::Value* v = getInnerLifetime(val.capturetok, id, errorPath, depth - 1))
return v;
if (errorPath)
errorPath->insert(errorPath->end(), val.errorPath.cbegin(), val.errorPath.cend());
return getInnerLifetime(val.tokvalue, id, errorPath, depth - 1);
if (const ValueFlow::Value* v = getInnerLifetime(val.tokvalue, id, errorPath, depth - 1))
return v;
continue;
}
if (!val.tokvalue->variable())
continue;
Expand Down
11 changes: 11 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6365,6 +6365,17 @@ class TestStl : public TestFixture {
"}\n",
true);
ASSERT_EQUALS("", errout_str());

// #13410
check("int f(std::vector<int>& v) {\n"
" const int* i = &*v.cbegin();\n"
" v.push_back(1);\n"
" return *i;\n"
"}\n",
true);
ASSERT_EQUALS(
"[test.cpp:1] -> [test.cpp:2] -> [test.cpp:1] -> [test.cpp:2] -> [test.cpp:2] -> [test.cpp:3] -> [test.cpp:1] -> [test.cpp:4]: (error) Using pointer to local variable 'v' that may be invalid.\n",
errout_str());
}

void invalidContainerLoop() {
Expand Down

0 comments on commit 5be15e3

Please sign in to comment.