Skip to content

Commit 89df134

Browse files
Fix #12109 Crash in calculate.h (#5587)
1 parent 309b4d8 commit 89df134

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

lib/calculate.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "mathlib.h"
2323
#include "errortypes.h"
24+
#include <limits>
2425
#include <string>
2526

2627
template<class T>
@@ -62,14 +63,14 @@ R calculate(const std::string& s, const T& x, const T& y, bool* error = nullptr)
6263
case '*':
6364
return wrap(x * y);
6465
case '/':
65-
if (isZero(y)) {
66+
if (isZero(y) || (std::is_integral<T>{} && std::is_signed<T>{} && isEqual(y, T(-1)) && isEqual(x, std::numeric_limits<T>::min()))) {
6667
if (error)
6768
*error = true;
6869
return R{};
6970
}
7071
return wrap(x / y);
7172
case '%':
72-
if (isZero(MathLib::bigint(y))) {
73+
if (isZero(MathLib::bigint(y)) || (std::is_integral<T>{} && std::is_signed<T>{} && isEqual(y, T(-1)) && isEqual(x, std::numeric_limits<T>::min()))) {
7374
if (error)
7475
*error = true;
7576
return R{};

test/testvalueflow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,8 @@ class TestValueFlow : public TestFixture {
911911
ASSERT(tokenValues(";-1>>10;",">>").empty());
912912
ASSERT(tokenValues(";10>>-1;",">>").empty());
913913
ASSERT(tokenValues(";10>>64;",">>").empty());
914+
ASSERT(tokenValues(";((-1) * 9223372036854775807LL - 1) / (-1);", "/").empty()); // #12109
915+
ASSERT_EQUALS(tokenValues(";((-1) * 9223372036854775807LL - 1) % (-1);", "%").size(), 1);
914916

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

0 commit comments

Comments
 (0)