Skip to content

Commit

Permalink
Fix #11067 FP mismatchingContainerIterator with container holding ite…
Browse files Browse the repository at this point in the history
…rators (#5915)
  • Loading branch information
chrchr-github committed Jan 29, 2024
1 parent 90d7f72 commit bef9e73
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ static bool isSameIteratorContainerExpression(const Token* tok1,
if (kind == ValueFlow::Value::LifetimeKind::Address) {
return isSameExpression(true, false, getAddressContainer(tok1), getAddressContainer(tok2), library, false, false);
}
return false;
return astContainerYield(tok2) == Library::Container::Yield::ITEM;
}

static ValueFlow::Value getLifetimeIteratorValue(const Token* tok, MathLib::bigint path = 0)
Expand Down Expand Up @@ -868,6 +868,8 @@ void CheckStl::mismatchingContainerIterator()
continue;
if (val.lifetimeKind != ValueFlow::Value::LifetimeKind::Iterator)
continue;
if (iterTok->str() == "*" && iterTok->astOperand1()->valueType() && iterTok->astOperand1()->valueType()->type == ValueType::ITERATOR)
continue;
if (isSameIteratorContainerExpression(tok, val.tokvalue, mSettings->library))
continue;
mismatchingContainerIteratorError(tok, iterTok, val.tokvalue);
Expand Down
26 changes: 26 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2135,6 +2135,32 @@ class TestStl : public TestFixture {
" s.v.erase(s.v.begin() + m);\n"
"}\n");
ASSERT_EQUALS("", errout.str());

// #11067
check("struct S {\n"
" std::vector<int> v;\n"
" std::list<std::vector<int>::const_iterator> li;\n"
" void f();\n"
"};\n"
"void S::f() {\n"
" v.erase(*li.begin());\n"
" li.pop_front();\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("void f(std::set<std::string>& a, std::stack<std::set<std::string>::iterator>& b) {\n"
" while (!b.empty()) {\n"
" a.erase(b.top());\n"
" b.pop();\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("void f(std::vector<int>& a, std::vector<std::vector<int>::iterator>& b) {\n"
" auto it = b.begin();\n"
" a.erase(*it);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

// Dereferencing invalid pointer
Expand Down

0 comments on commit bef9e73

Please sign in to comment.