Skip to content

Commit

Permalink
Fix #10009: bad overflow protection when taking out loans
Browse files Browse the repository at this point in the history
  • Loading branch information
rubidium42 authored and michicc committed Jan 28, 2023
1 parent 0cb3d85 commit bac7ad7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/core/overflowsafe_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ class OverflowSafeInt
inline constexpr bool operator <= (const int other) const { return !(*this > other); }

inline constexpr operator T () const { return this->m_value; }

static inline constexpr OverflowSafeInt<T> max() { return T_MAX; }
static inline constexpr OverflowSafeInt<T> min() { return T_MIN; }
};


Expand Down
6 changes: 4 additions & 2 deletions src/misc_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ CommandCost CmdIncreaseLoan(DoCommandFlag flags, LoanCommand cmd, Money amount)
break;
}

/* Overflow protection */
if (c->money + c->current_loan + loan < c->money) return CMD_ERROR;
/* In case adding the loan triggers the overflow protection of Money,
* we would essentially be losing money as taking and repaying the loan
* immediately would not get us back to the same bank balance anymore. */
if (c->money > Money::max() - loan) return CMD_ERROR;

if (flags & DC_EXEC) {
c->money += loan;
Expand Down

0 comments on commit bac7ad7

Please sign in to comment.