From f8fdc21fd4c423ce11697aeb7a7f00541108e69f Mon Sep 17 00:00:00 2001 From: firewave Date: Sun, 5 Jan 2025 14:14:45 +0100 Subject: [PATCH] calculate.h: cleaned up way too specific protections against potential FPE --- lib/calculate.h | 4 ++-- test/testvalueflow.cpp | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/calculate.h b/lib/calculate.h index 2b6c5ce2aaa..b86d31d9357 100644 --- a/lib/calculate.h +++ b/lib/calculate.h @@ -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{} && std::is_signed{} && isEqual(y, T(-1)) && isEqual(x, std::numeric_limits::min()))) { + if (isZero(y) || (std::is_signed{} && y < 0)) { if (error) *error = true; return R{}; } return wrap(x / y); case '%': - if (isZero(MathLib::bigint(y)) || (std::is_integral{} && std::is_signed{} && isEqual(y, T(-1)) && isEqual(x, std::numeric_limits::min()))) { + if (isZero(MathLib::bigint(y)) || (std::is_signed{} && MathLib::bigint(y) < 0)) { if (error) *error = true; return R{}; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 0cc2eb04ae5..6771ac7fdb3 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -951,15 +951,35 @@ class TestValueFlow : public TestFixture { ASSERT_EQUALS(10, valueOfTok("x = static_cast(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"