Skip to content

Commit

Permalink
calculate.h: cleaned up way too specific protections against potentia…
Browse files Browse the repository at this point in the history
…l FPE
  • Loading branch information
firewave committed Jan 7, 2025
1 parent ef3337d commit f8fdc21
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/calculate.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ R calculate(const std::string& s, const T& x, const T& y, bool* error = nullptr)
case '*':
return wrap(x * y);
case '/':
if (isZero(y) || (std::is_integral<T>{} && std::is_signed<T>{} && isEqual(y, T(-1)) && isEqual(x, std::numeric_limits<T>::min()))) {
if (isZero(y) || (std::is_signed<T>{} && y < 0)) {
if (error)
*error = true;
return R{};
}
return wrap(x / y);
case '%':
if (isZero(MathLib::bigint(y)) || (std::is_integral<T>{} && std::is_signed<T>{} && isEqual(y, T(-1)) && isEqual(x, std::numeric_limits<T>::min()))) {
if (isZero(MathLib::bigint(y)) || (std::is_signed<T>{} && MathLib::bigint(y) < 0)) {
if (error)
*error = true;
return R{};
Expand Down
24 changes: 22 additions & 2 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,15 +951,35 @@ class TestValueFlow : public TestFixture {
ASSERT_EQUALS(10, valueOfTok("x = static_cast<int>(10);", "( 10 )").intvalue);
ASSERT_EQUALS(0, valueOfTok("x = sizeof (struct {int a;}) * 0;", "*").intvalue);

// Don't calculate if there is UB
// Don't calculate or crash if there is UB or invalid operations
ASSERT(tokenValues(";-1<<10;","<<").empty());
ASSERT(tokenValues(";10<<-1;","<<").empty());
ASSERT(tokenValues(";10<<64;","<<").empty());
ASSERT(tokenValues(";-1>>10;",">>").empty());
ASSERT(tokenValues(";10>>-1;",">>").empty());
ASSERT(tokenValues(";10>>64;",">>").empty());
ASSERT_EQUALS(tokenValues(";1%-1;","%").size(), 1);
ASSERT_EQUALS(tokenValues(";1%-10;","%").size(), 1);
ASSERT_EQUALS(tokenValues(";1.5%-1;","%").size(), 1);
ASSERT_EQUALS(tokenValues(";1.5%-10;","%").size(), 1);
ASSERT(tokenValues(";1%-1.5;","%").empty());
ASSERT(tokenValues(";1%-10.5;","%").empty());
ASSERT(tokenValues(";1.5%-1.5;","%").empty());
ASSERT(tokenValues(";1.5%-10.5;","%").empty());
ASSERT(tokenValues(";1/-1;","/").empty());
ASSERT(tokenValues(";1/-10;","/").empty());
ASSERT(tokenValues(";1.5/-1;","/").empty());
ASSERT(tokenValues(";1.5/-10;","/").empty());
ASSERT(tokenValues(";1/-1.5;","/").empty());
ASSERT(tokenValues(";1/-10.5;","/").empty());
ASSERT(tokenValues(";1.5/-1.5;","/").empty());
ASSERT(tokenValues(";1.5/-10.5;","/").empty());
ASSERT(tokenValues(";1/0;","/").empty());
ASSERT(tokenValues(";1/0;","/").empty());
ASSERT(tokenValues(";1.5/0;","/").empty());
ASSERT(tokenValues(";1.5/0;","/").empty());
ASSERT(tokenValues(";((-1) * 9223372036854775807LL - 1) / (-1);", "/").empty()); // #12109
ASSERT_EQUALS(tokenValues(";((-1) * 9223372036854775807LL - 1) % (-1);", "%").size(), 1);
ASSERT_EQUALS(tokenValues(";((-1) * 9223372036854775807LL - 1) % (-1);", "%").size(), 1); // #12109

code = "float f(const uint16_t& value) {\n"
" const uint16_t uVal = value; \n"
Expand Down

0 comments on commit f8fdc21

Please sign in to comment.