Skip to content

Commit bef9e73

Browse files
Fix #11067 FP mismatchingContainerIterator with container holding iterators (#5915)
1 parent 90d7f72 commit bef9e73

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

lib/checkstl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ static bool isSameIteratorContainerExpression(const Token* tok1,
704704
if (kind == ValueFlow::Value::LifetimeKind::Address) {
705705
return isSameExpression(true, false, getAddressContainer(tok1), getAddressContainer(tok2), library, false, false);
706706
}
707-
return false;
707+
return astContainerYield(tok2) == Library::Container::Yield::ITEM;
708708
}
709709

710710
static ValueFlow::Value getLifetimeIteratorValue(const Token* tok, MathLib::bigint path = 0)
@@ -868,6 +868,8 @@ void CheckStl::mismatchingContainerIterator()
868868
continue;
869869
if (val.lifetimeKind != ValueFlow::Value::LifetimeKind::Iterator)
870870
continue;
871+
if (iterTok->str() == "*" && iterTok->astOperand1()->valueType() && iterTok->astOperand1()->valueType()->type == ValueType::ITERATOR)
872+
continue;
871873
if (isSameIteratorContainerExpression(tok, val.tokvalue, mSettings->library))
872874
continue;
873875
mismatchingContainerIteratorError(tok, iterTok, val.tokvalue);

test/teststl.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,32 @@ class TestStl : public TestFixture {
21352135
" s.v.erase(s.v.begin() + m);\n"
21362136
"}\n");
21372137
ASSERT_EQUALS("", errout.str());
2138+
2139+
// #11067
2140+
check("struct S {\n"
2141+
" std::vector<int> v;\n"
2142+
" std::list<std::vector<int>::const_iterator> li;\n"
2143+
" void f();\n"
2144+
"};\n"
2145+
"void S::f() {\n"
2146+
" v.erase(*li.begin());\n"
2147+
" li.pop_front();\n"
2148+
"}\n");
2149+
ASSERT_EQUALS("", errout.str());
2150+
2151+
check("void f(std::set<std::string>& a, std::stack<std::set<std::string>::iterator>& b) {\n"
2152+
" while (!b.empty()) {\n"
2153+
" a.erase(b.top());\n"
2154+
" b.pop();\n"
2155+
" }\n"
2156+
"}\n");
2157+
ASSERT_EQUALS("", errout.str());
2158+
2159+
check("void f(std::vector<int>& a, std::vector<std::vector<int>::iterator>& b) {\n"
2160+
" auto it = b.begin();\n"
2161+
" a.erase(*it);\n"
2162+
"}\n");
2163+
ASSERT_EQUALS("", errout.str());
21382164
}
21392165

21402166
// Dereferencing invalid pointer

0 commit comments

Comments
 (0)