Fix issue 9973: False positive: mismatching iterator container, reference (#2893)

This commit is contained in:
Paul Fultz II 2020-11-10 02:38:05 -06:00 committed by GitHub
parent bd5fc4b579
commit ae6f611df0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View File

@ -796,9 +796,15 @@ void CheckStl::mismatchingContainerIterator()
continue;
if (val.lifetimeKind != ValueFlow::Value::LifetimeKind::Iterator)
continue;
if (isSameExpression(true, false, tok, val.tokvalue, mSettings->library, false, false))
continue;
mismatchingContainerIteratorError(tok, iterTok);
for (const LifetimeToken& lt:getLifetimeTokens(tok)) {
if (lt.inconclusive)
continue;
const Token* contTok = lt.token;
if (isSameExpression(true, false, contTok, val.tokvalue, mSettings->library, false, false))
continue;
mismatchingContainerIteratorError(tok, iterTok);
}
}
}
}

View File

@ -1351,6 +1351,20 @@ private:
" return a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Iterator 'b.begin()' from different container 'a' are used together.\n", errout.str());
// #9973
check("void f() {\n"
" std::list<int> l1;\n"
" std::list<int> l2;\n"
" std::list<int>& l = l2;\n"
" for (auto it = l.begin(); it != l.end(); ++it) {\n"
" if (*it == 1) {\n"
" l.erase(it);\n"
" break;\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
// Dereferencing invalid pointer