Skip to content

Commit

Permalink
Fix #12641 FP mismatchingContainers with repeated ternary (danmar#6337)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Apr 24, 2024
1 parent f7949e7 commit 94f28a2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,12 +727,18 @@ static bool isSameIteratorContainerExpression(const Token* tok1,

static ValueFlow::Value getLifetimeIteratorValue(const Token* tok, MathLib::bigint path = 0)
{
auto findIterVal = [](const std::vector<ValueFlow::Value>& values, const std::vector<ValueFlow::Value>::const_iterator beg) {
return std::find_if(beg, values.cend(), [](const ValueFlow::Value& v) {
return v.lifetimeKind == ValueFlow::Value::LifetimeKind::Iterator;
});
};
std::vector<ValueFlow::Value> values = ValueFlow::getLifetimeObjValues(tok, false, path);
auto it = std::find_if(values.cbegin(), values.cend(), [](const ValueFlow::Value& v) {
return v.lifetimeKind == ValueFlow::Value::LifetimeKind::Iterator;
});
if (it != values.end())
return *it;
auto it = findIterVal(values, values.begin());
if (it != values.end()) {
auto it2 = findIterVal(values, it + 1);
if (it2 == values.cend())
return *it;
}
if (values.size() == 1)
return values.front();
return ValueFlow::Value{};
Expand Down
17 changes: 17 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class TestStl : public TestFixture {
TEST_CASE(iterator27); // #10378
TEST_CASE(iterator28); // #10450
TEST_CASE(iterator29);
TEST_CASE(iterator30);
TEST_CASE(iteratorExpression);
TEST_CASE(iteratorSameExpression);
TEST_CASE(mismatchingContainerIterator);
Expand Down Expand Up @@ -1869,6 +1870,22 @@ class TestStl : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void iterator30()
{
check("struct S {\n" // #12641
" bool b;\n"
" std::list<int> A, B;\n"
" void f();\n"
"};\n"
"void S::f() {\n"
" std::list<int>::iterator i = (b ? B : A).begin();\n"
" while (i != (b ? B : A).end()) {\n"
" ++i;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void iteratorExpression() {
check("std::vector<int>& f();\n"
"std::vector<int>& g();\n"
Expand Down

0 comments on commit 94f28a2

Please sign in to comment.