Skip to content

Commit 5be15e3

Browse files
authored
Fix 13410: False negative: invalidContainer when taking address of dereferenced iterator (danmar#7109)
1 parent 1093382 commit 5be15e3

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/checkstl.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,10 +1094,13 @@ static const ValueFlow::Value* getInnerLifetime(const Token* tok,
10941094
if (val.isInconclusive())
10951095
return nullptr;
10961096
if (val.capturetok)
1097-
return getInnerLifetime(val.capturetok, id, errorPath, depth - 1);
1097+
if (const ValueFlow::Value* v = getInnerLifetime(val.capturetok, id, errorPath, depth - 1))
1098+
return v;
10981099
if (errorPath)
10991100
errorPath->insert(errorPath->end(), val.errorPath.cbegin(), val.errorPath.cend());
1100-
return getInnerLifetime(val.tokvalue, id, errorPath, depth - 1);
1101+
if (const ValueFlow::Value* v = getInnerLifetime(val.tokvalue, id, errorPath, depth - 1))
1102+
return v;
1103+
continue;
11011104
}
11021105
if (!val.tokvalue->variable())
11031106
continue;

test/teststl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6365,6 +6365,17 @@ class TestStl : public TestFixture {
63656365
"}\n",
63666366
true);
63676367
ASSERT_EQUALS("", errout_str());
6368+
6369+
// #13410
6370+
check("int f(std::vector<int>& v) {\n"
6371+
" const int* i = &*v.cbegin();\n"
6372+
" v.push_back(1);\n"
6373+
" return *i;\n"
6374+
"}\n",
6375+
true);
6376+
ASSERT_EQUALS(
6377+
"[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",
6378+
errout_str());
63686379
}
63696380

63706381
void invalidContainerLoop() {

0 commit comments

Comments
 (0)