Skip to content

Commit

Permalink
Fix #12109 Crash in calculate.h (#5587)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Oct 24, 2023
1 parent 309b4d8 commit 89df134
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/calculate.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "mathlib.h"
#include "errortypes.h"
#include <limits>
#include <string>

template<class T>
Expand Down Expand Up @@ -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{};
Expand Down
2 changes: 2 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,8 @@ class TestValueFlow : public TestFixture {
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"
Expand Down

0 comments on commit 89df134

Please sign in to comment.