Fix issue 10306: FP knownConditionTrueFalse with modulo result converted to bool (#3282)

This commit is contained in:
Paul Fultz II 2021-06-03 00:26:36 -05:00 committed by GitHub
parent 8647e644a5
commit 548ec10824
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -723,6 +723,8 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value, const Setti
for (const ValueFlow::Value &val : tok->values()) { for (const ValueFlow::Value &val : tok->values()) {
if (!val.isIntValue()) if (!val.isIntValue())
continue; continue;
if (val.isImpossible() && val.intvalue != 0)
continue;
ValueFlow::Value v(val); ValueFlow::Value v(val);
v.intvalue = !v.intvalue; v.intvalue = !v.intvalue;
setTokenValue(parent, v, settings); setTokenValue(parent, v, settings);

View File

@ -141,6 +141,7 @@ private:
TEST_CASE(valueFlowUnknownMixedOperators); TEST_CASE(valueFlowUnknownMixedOperators);
TEST_CASE(valueFlowIdempotent); TEST_CASE(valueFlowIdempotent);
TEST_CASE(valueFlowUnsigned); TEST_CASE(valueFlowUnsigned);
TEST_CASE(valueFlowMod);
} }
static bool isNotTokValue(const ValueFlow::Value &val) { static bool isNotTokValue(const ValueFlow::Value &val) {
@ -2643,6 +2644,20 @@ private:
ASSERT_EQUALS(false, testValueOfXKnown(code, 5U, 1)); ASSERT_EQUALS(false, testValueOfXKnown(code, 5U, 1));
ASSERT_EQUALS(false, testValueOfXImpossible(code, 5U, 0)); ASSERT_EQUALS(false, testValueOfXImpossible(code, 5U, 0));
ASSERT_EQUALS(false, testValueOfXImpossible(code, 5U, 1)); ASSERT_EQUALS(false, testValueOfXImpossible(code, 5U, 1));
code = "auto f(int i) {\n"
" if (i == 0) return;\n"
" auto x = !i;\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXImpossible(code, 4U, 1));
code = "auto f(int i) {\n"
" if (i == 1) return;\n"
" auto x = !i;\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfXImpossible(code, 4U, 0));
} }
void valueFlowAfterConditionExpr() { void valueFlowAfterConditionExpr() {
@ -5638,6 +5653,23 @@ private:
ASSERT_EQUALS(false, testValueOfXImpossible(code, 4U, 0)); ASSERT_EQUALS(false, testValueOfXImpossible(code, 4U, 0));
ASSERT_EQUALS(true, testValueOfXImpossible(code, 4U, -1)); ASSERT_EQUALS(true, testValueOfXImpossible(code, 4U, -1));
} }
void valueFlowMod() {
const char *code;
code = "auto f(int i) {\n"
" auto x = i % 2;\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXImpossible(code, 3U, 2));
code = "auto f(int i) {\n"
" auto x = !(i % 2);\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfXImpossible(code, 3U, 0));
ASSERT_EQUALS(false, testValueOfXImpossible(code, 3U, 1));
}
}; };
REGISTER_TEST(TestValueFlow) REGISTER_TEST(TestValueFlow)