From 548ec108244f674d8f0708581f77df80f7e60a6f Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Thu, 3 Jun 2021 00:26:36 -0500 Subject: [PATCH] Fix issue 10306: FP knownConditionTrueFalse with modulo result converted to bool (#3282) --- lib/valueflow.cpp | 2 ++ test/testvalueflow.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 18590b8a8..c38575bb1 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -723,6 +723,8 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value, const Setti for (const ValueFlow::Value &val : tok->values()) { if (!val.isIntValue()) continue; + if (val.isImpossible() && val.intvalue != 0) + continue; ValueFlow::Value v(val); v.intvalue = !v.intvalue; setTokenValue(parent, v, settings); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index a9e80dabc..772b4e85a 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -141,6 +141,7 @@ private: TEST_CASE(valueFlowUnknownMixedOperators); TEST_CASE(valueFlowIdempotent); TEST_CASE(valueFlowUnsigned); + TEST_CASE(valueFlowMod); } static bool isNotTokValue(const ValueFlow::Value &val) { @@ -2643,6 +2644,20 @@ private: ASSERT_EQUALS(false, testValueOfXKnown(code, 5U, 1)); ASSERT_EQUALS(false, testValueOfXImpossible(code, 5U, 0)); 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() { @@ -5638,6 +5653,23 @@ private: ASSERT_EQUALS(false, testValueOfXImpossible(code, 4U, 0)); 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)