Fix issue 10226: FP: redundant condition or invalid iterator (#3195)

This commit is contained in:
Paul Fultz II 2021-04-06 04:04:37 -05:00 committed by GitHub
parent 93873be81a
commit e0f9627201
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -4476,6 +4476,7 @@ struct ConditionHandler {
// start token of conditional code
Token* startTokens[] = {nullptr, nullptr};
bool inverted = false;
// if astParent is "!" we need to invert codeblock
{
const Token* tok2 = tok;
@ -4484,6 +4485,7 @@ struct ConditionHandler {
while (parent && parent->str() == "&&")
parent = parent->astParent();
if (parent && (parent->str() == "!" || Token::simpleMatch(parent, "== false"))) {
inverted = true;
std::swap(thenValues, elseValues);
}
tok2 = parent;
@ -4568,7 +4570,7 @@ struct ConditionHandler {
}
if (dead_if || dead_else) {
if (Token::Match(tok->astParent(), "&&|&")) {
if (!inverted && Token::Match(tok->astParent(), "&&|&")) {
values.remove_if(std::mem_fn(&ValueFlow::Value::isImpossible));
changeKnownToPossible(values);
} else {

View File

@ -3805,6 +3805,15 @@ private:
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("int f(std::vector<int>::iterator it, const std::vector<int>& vector) {\n"
" if (!(it != vector.end() && it != vector.begin()))\n"
" throw std::out_of_range();\n"
" if (it != vector.end() && *it == 0)\n"
" return -1;\n"
" return *it;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void dereferenceInvalidIterator2() {