Merge pull request #2793 from Ken-Patrick/mixedoperators

Fix false positives with condition with || and &&
This commit is contained in:
Daniel Marjamäki 2020-09-11 10:11:31 +02:00 committed by GitHub
commit 600538a325
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 0 deletions

View File

@ -4424,6 +4424,31 @@ struct ValueFlowConditionHandler {
}
}
{
const Token *tok2 = tok;
std::string op;
bool mixedOperators = false;
while (tok2->astParent()) {
const Token *parent = tok2->astParent();
if (Token::Match(parent, "%oror%|&&")) {
if (op.empty()) {
op = parent->str() == "&&" ? "&&" : "||";
} else if (op != parent->str()) {
mixedOperators = true;
break;
}
}
if (parent->str()=="!") {
op = (op == "&&" ? "||" : "&&");
}
tok2 = parent;
}
if (mixedOperators) {
continue;
}
}
if (top && Token::Match(top->previous(), "if|while (") && !top->previous()->isExpandedMacro()) {
// does condition reassign variable?
if (tok != top->astOperand2() && Token::Match(top->astOperand2(), "%oror%|&&") &&

View File

@ -138,6 +138,8 @@ private:
TEST_CASE(valueFlowCrash);
TEST_CASE(valueFlowHang);
TEST_CASE(valueFlowCrashConstructorInitialization);
TEST_CASE(valueFlowUnknownMixedOperators);
}
static bool isNotTokValue(const ValueFlow::Value &val) {
@ -4827,6 +4829,20 @@ private:
"}";
valueOfTok(code, "path");
}
void valueFlowUnknownMixedOperators() {
const char *code= "int f(int a, int b, bool x) {\n"
" if (a == 1 && (!(b == 2 && x))) {\n"
" } else {\n"
" if (x) {\n"
" }\n"
" }\n"
"\n"
" return 0;\n"
"}" ;
ASSERT_EQUALS(false, testValueOfXKnown(code, 4U, 1));
}
};
REGISTER_TEST(TestValueFlow)