Fix #12109 Crash in calculate.h (#5587)

This commit is contained in:
chrchr-github 2023-10-24 10:05:32 +02:00 committed by GitHub
parent 309b4d8501
commit 89df134fed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 2 deletions

View File

@ -21,6 +21,7 @@
#include "mathlib.h"
#include "errortypes.h"
#include <limits>
#include <string>
template<class T>
@ -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<T>{} && std::is_signed<T>{} && isEqual(y, T(-1)) && isEqual(x, std::numeric_limits<T>::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<T>{} && std::is_signed<T>{} && isEqual(y, T(-1)) && isEqual(x, std::numeric_limits<T>::min()))) {
if (error)
*error = true;
return R{};

View File

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