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
This commit is contained in:
chrchr-github 2022-10-31 15:18:25 +01:00 committed by GitHub
parent e8606a5e5a
commit cf8051b7e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 7 deletions

View File

@ -468,9 +468,6 @@ double MathLib::toDoubleNumber(const std::string &str)
}
if (isIntHex(str))
return static_cast<double>(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

View File

@ -26,6 +26,7 @@
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <functional>
#include <list>
#include <string>
@ -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:

View File

@ -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 {

View File

@ -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() {