From cf8051b7e2e92d909c29401765c28e8056ca438e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 31 Oct 2022 15:18:25 +0100 Subject: [PATCH] Fix #11368 FP "Same value in both branches of ternary operator" on plus and minus zero (#4569) Fix #11368 FP "Same value in both branches of ternary operator" on plus and minus zero --- lib/mathlib.cpp | 5 +---- lib/valueflow.h | 4 ++-- test/testmathlib.cpp | 2 +- test/testother.cpp | 5 +++++ 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index b89399b55..5f0892ae3 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -468,9 +468,6 @@ double MathLib::toDoubleNumber(const std::string &str) } if (isIntHex(str)) return static_cast(toLongNumber(str)); - // nullcheck - if (isNullValue(str)) - return 0.0; #ifdef _LIBCPP_VERSION if (isFloat(str)) // Workaround libc++ bug at http://llvm.org/bugs/show_bug.cgi?id=17782 // TODO : handle locale @@ -1053,7 +1050,7 @@ std::string MathLib::divide(const std::string &first, const std::string &second) } else if (isNullValue(second)) { if (isNullValue(first)) return "nan.0"; - return isPositive(first) ? "inf.0" : "-inf.0"; + return isPositive(first) == isPositive(second) ? "inf.0" : "-inf.0"; } return toString(toDoubleNumber(first) / toDoubleNumber(second)); #endif diff --git a/lib/valueflow.h b/lib/valueflow.h index e05077498..ca5202f84 100644 --- a/lib/valueflow.h +++ b/lib/valueflow.h @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -130,8 +131,7 @@ namespace ValueFlow { return false; break; case ValueType::FLOAT: - // TODO: Write some better comparison - if (floatValue > rhs.floatValue || floatValue < rhs.floatValue) + if (floatValue > rhs.floatValue || floatValue < rhs.floatValue || std::signbit(floatValue) != std::signbit(rhs.floatValue)) return false; break; case ValueType::MOVED: diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 5f81b5587..db3992747 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -977,7 +977,7 @@ private: ASSERT_EQUALS("inf.0", MathLib::divide("3.0", "0.f")); // inf (#5875) ASSERT_EQUALS("-inf.0", MathLib::divide("-3.0", "0.0")); // -inf (#5142) ASSERT_EQUALS("-inf.0", MathLib::divide("-3.0", "0.0f")); // -inf (#5142) - ASSERT_EQUALS("-inf.0", MathLib::divide("-3.0", "-0.0f")); // inf (#5142) + ASSERT_EQUALS("inf.0", MathLib::divide("-3.0", "-0.0f")); // inf (#5142) } void isdec() const { diff --git a/test/testother.cpp b/test/testother.cpp index b0008c667..8f62e3076 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6463,6 +6463,11 @@ private: " return f;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("float f(float x) {\n" // # 11368 + " return (x >= 0.0) ? 0.0 : -0.0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void duplicateExpressionTemplate() {