From 7c986fbef1fe6dcdc6ee0f69d3ed09695509bdf6 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Thu, 8 Sep 2022 12:59:02 -0500 Subject: [PATCH] Fix 11203: false positive: knownConditionTrueFalse 'always false' when comparing integer with floating-point (#4350) --- lib/valueflow.cpp | 14 ++++++++------ test/testcondition.cpp | 10 ++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 14fbd52a9..0a58ada5a 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -836,10 +836,12 @@ static void setTokenValue(Token* tok, } const double floatValue1 = value1.isFloatValue() ? value1.floatValue : value1.intvalue; const double floatValue2 = value2.isFloatValue() ? value2.floatValue : value2.intvalue; - const MathLib::bigint intValue1 = - value1.isFloatValue() ? std::llround(value1.floatValue) : value1.intvalue; - const MathLib::bigint intValue2 = - value2.isFloatValue() ? std::llround(value2.floatValue) : value2.intvalue; + const auto intValue1 = [&]() -> MathLib::bigint { + return value1.isFloatValue() ? static_cast(value1.floatValue) : value1.intvalue; + }; + const auto intValue2 = [&]() -> MathLib::bigint { + return value2.isFloatValue() ? static_cast(value2.floatValue) : value2.intvalue; + }; if ((value1.isFloatValue() || value2.isFloatValue()) && Token::Match(parent, "&|^|%|<<|>>|==|!=|%or%")) continue; if (Token::Match(parent, "==|!=")) { @@ -850,7 +852,7 @@ static void setTokenValue(Token* tok, result.intvalue = 1; } else if (value1.isIntValue() && value2.isIntValue()) { bool error = false; - result.intvalue = calculate(parent->str(), intValue1, intValue2, &error); + result.intvalue = calculate(parent->str(), intValue1(), intValue2(), &error); if (error) continue; } else { @@ -869,7 +871,7 @@ static void setTokenValue(Token* tok, if (result.isFloatValue()) { result.floatValue = calculate(parent->str(), floatValue1, floatValue2, &error); } else { - result.intvalue = calculate(parent->str(), intValue1, intValue2, &error); + result.intvalue = calculate(parent->str(), intValue1(), intValue2(), &error); } if (error) continue; diff --git a/test/testcondition.cpp b/test/testcondition.cpp index eaaeb4bb7..4602a89a3 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4283,6 +4283,16 @@ private: "};\n"); ASSERT_EQUALS("", errout.str()); + // #11203 + check("void f() {\n" + " int i = 10;\n" + " if(i > 9.9){}\n" + " float f = 9.9f;\n" + " if(f < 10) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'i>9.9' is always true\n" + "[test.cpp:5]: (style) Condition 'f<10' is always true\n", + errout.str()); check("constexpr int f() {\n" // #11238 " return 1;\n" "}\n"