Skip to content

Commit cf8051b

Browse files
Fix #11368 FP "Same value in both branches of ternary operator" on plus and minus zero (cppcheck-opensource#4569)
Fix #11368 FP "Same value in both branches of ternary operator" on plus and minus zero
1 parent e8606a5 commit cf8051b

4 files changed

Lines changed: 9 additions & 7 deletions

File tree

lib/mathlib.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,6 @@ double MathLib::toDoubleNumber(const std::string &str)
468468
}
469469
if (isIntHex(str))
470470
return static_cast<double>(toLongNumber(str));
471-
// nullcheck
472-
if (isNullValue(str))
473-
return 0.0;
474471
#ifdef _LIBCPP_VERSION
475472
if (isFloat(str)) // Workaround libc++ bug at http://llvm.org/bugs/show_bug.cgi?id=17782
476473
// TODO : handle locale
@@ -1053,7 +1050,7 @@ std::string MathLib::divide(const std::string &first, const std::string &second)
10531050
} else if (isNullValue(second)) {
10541051
if (isNullValue(first))
10551052
return "nan.0";
1056-
return isPositive(first) ? "inf.0" : "-inf.0";
1053+
return isPositive(first) == isPositive(second) ? "inf.0" : "-inf.0";
10571054
}
10581055
return toString(toDoubleNumber(first) / toDoubleNumber(second));
10591056
#endif

lib/valueflow.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <cassert>
2828
#include <cstdlib>
29+
#include <cmath>
2930
#include <functional>
3031
#include <list>
3132
#include <string>
@@ -130,8 +131,7 @@ namespace ValueFlow {
130131
return false;
131132
break;
132133
case ValueType::FLOAT:
133-
// TODO: Write some better comparison
134-
if (floatValue > rhs.floatValue || floatValue < rhs.floatValue)
134+
if (floatValue > rhs.floatValue || floatValue < rhs.floatValue || std::signbit(floatValue) != std::signbit(rhs.floatValue))
135135
return false;
136136
break;
137137
case ValueType::MOVED:

test/testmathlib.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ class TestMathLib : public TestFixture {
977977
ASSERT_EQUALS("inf.0", MathLib::divide("3.0", "0.f")); // inf (#5875)
978978
ASSERT_EQUALS("-inf.0", MathLib::divide("-3.0", "0.0")); // -inf (#5142)
979979
ASSERT_EQUALS("-inf.0", MathLib::divide("-3.0", "0.0f")); // -inf (#5142)
980-
ASSERT_EQUALS("-inf.0", MathLib::divide("-3.0", "-0.0f")); // inf (#5142)
980+
ASSERT_EQUALS("inf.0", MathLib::divide("-3.0", "-0.0f")); // inf (#5142)
981981
}
982982

983983
void isdec() const {

test/testother.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6463,6 +6463,11 @@ class TestOther : public TestFixture {
64636463
" return f;\n"
64646464
"}\n");
64656465
ASSERT_EQUALS("", errout.str());
6466+
6467+
check("float f(float x) {\n" // # 11368
6468+
" return (x >= 0.0) ? 0.0 : -0.0;\n"
6469+
"}\n");
6470+
ASSERT_EQUALS("", errout.str());
64666471
}
64676472

64686473
void duplicateExpressionTemplate() {

0 commit comments

Comments
 (0)