Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper overflow handling #2734

Merged
merged 7 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 61 additions & 5 deletions src/game_variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,71 @@ constexpr Var_t VarSet(Var_t o, Var_t n) {
}

constexpr Var_t VarAdd(Var_t l, Var_t r) {
return l + r;
Var_t res = 0;

#ifdef _MSC_VER
res = l + r;
if (res < 0 && l > 0 && r > 0) {
return std::numeric_limits<Var_t>::max();
} else if (res > 0 && l < 0 && r < 0) {
return std::numeric_limits<Var_t>::min();
}
#else
if (EP_UNLIKELY(__builtin_add_overflow(l, r, &res))) {
if (l >= 0 && r >= 0) {
return std::numeric_limits<Var_t>::max();
}
return std::numeric_limits<Var_t>::min();
}
#endif

return res;
}

constexpr Var_t VarSub(Var_t l, Var_t r) {
return l - r;
Var_t res = 0;

#ifdef _MSC_VER
res = l - r;
if (res < 0 && l > 0 && r < 0) {
return std::numeric_limits<Var_t>::max();
} else if (res > 0 && l < 0 && r > 0) {
return std::numeric_limits<Var_t>::min();
}
#else
if (EP_UNLIKELY(__builtin_sub_overflow(l, r, &res))) {
if (r < 0) {
return std::numeric_limits<Var_t>::max();
}
return std::numeric_limits<Var_t>::min();
}
#endif

return res;
}

constexpr Var_t VarMult(Var_t l, Var_t r) {
return l * r;
Var_t res = 0;

#ifdef _MSC_VER
res = l * r;
if (l != 0 && res / l != r) {
if ((l > 0 && r > 0) || (l < 0 && r < 0)) {
return std::numeric_limits<Var_t>::max();
} else {
return std::numeric_limits<Var_t>::min();
}
}
#else
if (EP_UNLIKELY(__builtin_mul_overflow(l, r, &res))) {
if ((l > 0 && r > 0) || (l < 0 && r < 0)) {
return std::numeric_limits<Var_t>::max();
}
return std::numeric_limits<Var_t>::min();
}
#endif

return res;
}

constexpr Var_t VarDiv(Var_t n, Var_t d) {
Expand Down Expand Up @@ -560,6 +616,6 @@ StringView Game_Variables::GetName(int _id) const {
}

int Game_Variables::GetMaxDigits() const {
auto val = std::max(std::abs(_max), std::abs(_min));
return std::log10(val) + 1;
auto val = std::max(std::llabs(_max), std::llabs(_min));
return static_cast<int>(std::log10(val) + 1);
}
12 changes: 10 additions & 2 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,11 +885,19 @@ void Player::ResetGameObjects() {

auto min_var = lcf::Data::system.easyrpg_variable_min_value;
if (min_var == 0) {
min_var = Player::IsRPG2k3() ? Game_Variables::min_2k3 : Game_Variables::min_2k;
if (Player::IsPatchManiac()) {
min_var = std::numeric_limits<Game_Variables::Var_t>::min();
} else {
min_var = Player::IsRPG2k3() ? Game_Variables::min_2k3 : Game_Variables::min_2k;
}
}
auto max_var = lcf::Data::system.easyrpg_variable_max_value;
if (max_var == 0) {
max_var = Player::IsRPG2k3() ? Game_Variables::max_2k3 : Game_Variables::max_2k;
if (Player::IsPatchManiac()) {
max_var = std::numeric_limits<Game_Variables::Var_t>::max();
} else {
max_var = Player::IsRPG2k3() ? Game_Variables::max_2k3 : Game_Variables::max_2k;
}
}
Main_Data::game_variables = std::make_unique<Game_Variables>(min_var, max_var);

Expand Down
51 changes: 41 additions & 10 deletions src/window_numberinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,28 @@ void Window_NumberInput::Refresh() {
}
}

int Window_NumberInput::GetNumber() {
return number * (plus ? 1 : -1);
int Window_NumberInput::GetNumber() const {
if (plus) {
if (number > std::numeric_limits<int>::max()) {
return std::numeric_limits<int>::max();
} else {
return static_cast<int>(number);
}
} else {
if (number * -1 < std::numeric_limits<int>::min()) {
return std::numeric_limits<int>::min();
} else {
return static_cast<int>(-number);
}
}
}

void Window_NumberInput::SetNumber(int inumber) {
int num = 1;
int64_t num = 1;
for (int i = 0; i < digits_max; ++i) {
num *= 10;
}
number = min(max(abs(inumber), 0), num - 1);
number = Utils::Clamp<int64_t>(std::llabs(inumber), 0, num - 1);
ResetIndex();

plus = inumber >= 0;
Expand All @@ -80,14 +92,13 @@ void Window_NumberInput::SetNumber(int inumber) {
Refresh();
}

int Window_NumberInput::GetMaxDigits() {
int Window_NumberInput::GetMaxDigits() const {
return digits_max;
}

void Window_NumberInput::SetMaxDigits(int idigits_max) {
// At least 7 digits because of gold input in debug scene
// (free space and 6 digits for the gold value)
int top = std::max(7, idigits_max);
// Up to 10 digits (highest 32 bit number)
int top = std::max(10, idigits_max);
digits_max =
(idigits_max > top) ? top :
(idigits_max <= 0) ? 1 :
Expand All @@ -97,7 +108,7 @@ void Window_NumberInput::SetMaxDigits(int idigits_max) {
Refresh();
}

bool Window_NumberInput::GetShowOperator() {
bool Window_NumberInput::GetShowOperator() const {
return show_operator;
}

Expand All @@ -122,7 +133,7 @@ void Window_NumberInput::Update() {
for (int i = 0; i < (digits_max - 1 - (int)index + (int)show_operator); ++i) {
place *= 10;
}
int n = number / place % 10;
int64_t n = number / place % 10;
number -= n * place;
if (Input::IsRepeated(Input::UP)) {
n = (n + 1) % 10;
Expand Down Expand Up @@ -150,6 +161,26 @@ void Window_NumberInput::Update() {
index = (index + digits_max - 1 + (int)show_operator) % (digits_max + (int)show_operator);
}

// Extension: Allow number input through numpad
if (!show_operator || index > 0) {
for (int btn = static_cast<int>(Input::N0); btn <= static_cast<int>(Input::N9); ++btn) {
if (Input::IsTriggered(static_cast<Input::InputButton>(btn))) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cursor));

int place = 1;
for (int i = 0; i < (digits_max - 1 - (int)index + (int)show_operator); ++i) {
place *= 10;
}
int64_t n = number / place % 10;
number -= n * place;
number += (btn - static_cast<int>(Input::N0)) * static_cast<int64_t>(place);
index = (index + 1) % (digits_max + (int)show_operator);
Refresh();
break;
}
}
}

UpdateCursorRect();
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/window_numberinput.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Window_NumberInput : public Window_Selectable {
*
* @return the currently input number.
*/
int GetNumber();
int GetNumber() const;

/**
* Sets a new number value.
Expand All @@ -61,7 +61,7 @@ class Window_NumberInput : public Window_Selectable {
*
* @return number of displayed digits.
*/
int GetMaxDigits();
int GetMaxDigits() const;

/**
* Sets the maximal displayed digits.
Expand All @@ -76,7 +76,7 @@ class Window_NumberInput : public Window_Selectable {
*
* @return the current operator state
*/
bool GetShowOperator();
bool GetShowOperator() const;

/**
* Enables or Disables the +- operator before the numbers.
Expand All @@ -97,7 +97,7 @@ class Window_NumberInput : public Window_Selectable {
void Update() override;

protected:
int number;
int64_t number;
int digits_max;
int cursor_width;
int index;
Expand Down
42 changes: 42 additions & 0 deletions tests/variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,48 @@ TEST_CASE("RangeRandom") {
REQUIRE_NE(first_diff, 0);
}

TEST_CASE("Overflow/Underflow") {
lcf::Data::variables.resize(max_vars);

auto _min = std::numeric_limits<Game_Variables::Var_t>::min();
auto _max = std::numeric_limits<Game_Variables::Var_t>::max();

Game_Variables v(_min, _max);
v.SetWarning(0);

v.Set(1, _max);
v.Add(1, 1);
REQUIRE(v.Get(1) == _max);

v.Set(1, _min);
v.Add(1, -1);
REQUIRE(v.Get(1) == _min);

v.Set(1, _max);
v.Sub(1, -1);
REQUIRE(v.Get(1) == _max);

v.Set(1, _min);
v.Sub(1, 1);
REQUIRE(v.Get(1) == _min);

v.Set(1, _max);
v.Mult(1, 2);
REQUIRE(v.Get(1) == _max);

v.Set(1, _min);
v.Mult(1, -2);
REQUIRE(v.Get(1) == _max);

v.Set(1, _max);
v.Mult(1, -2);
REQUIRE(v.Get(1) == _min);

v.Set(1, _min);
v.Mult(1, 2);
REQUIRE(v.Get(1) == _min);
}

TEST_CASE("Enumerate") {
auto s = make();

Expand Down