From 89df134fed1393e600864f0548ab4d835cd0c24c Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 24 Oct 2023 10:05:32 +0200 Subject: [PATCH] Fix #12109 Crash in calculate.h (#5587) --- lib/calculate.h | 5 +++-- test/testvalueflow.cpp | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/calculate.h b/lib/calculate.h index 5d4319dac..76117d6e7 100644 --- a/lib/calculate.h +++ b/lib/calculate.h @@ -21,6 +21,7 @@ #include "mathlib.h" #include "errortypes.h" +#include #include template @@ -62,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)) { + if (isZero(y) || (std::is_integral{} && std::is_signed{} && isEqual(y, T(-1)) && isEqual(x, std::numeric_limits::min()))) { if (error) *error = true; return R{}; } return wrap(x / y); case '%': - if (isZero(MathLib::bigint(y))) { + if (isZero(MathLib::bigint(y)) || (std::is_integral{} && std::is_signed{} && isEqual(y, T(-1)) && isEqual(x, std::numeric_limits::min()))) { if (error) *error = true; return R{}; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index ce8c25b3e..94479182c 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -911,6 +911,8 @@ private: ASSERT(tokenValues(";-1>>10;",">>").empty()); ASSERT(tokenValues(";10>>-1;",">>").empty()); ASSERT(tokenValues(";10>>64;",">>").empty()); + ASSERT(tokenValues(";((-1) * 9223372036854775807LL - 1) / (-1);", "/").empty()); // #12109 + ASSERT_EQUALS(tokenValues(";((-1) * 9223372036854775807LL - 1) % (-1);", "%").size(), 1); code = "float f(const uint16_t& value) {\n" " const uint16_t uVal = value; \n"