From 34b05465d5683b60c2af68a22063bf2fbd82250e Mon Sep 17 00:00:00 2001 From: merni-ns <66267867+merni-ns@users.noreply.github.com> Date: Wed, 7 Feb 2024 00:24:38 +0530 Subject: [PATCH] Fix 5a88027: [Script] Avoid overflow in scripts when infinite money is enabled (#12016) --- src/script/api/script_company.cpp | 2 ++ src/script/api/script_company.hpp | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/script/api/script_company.cpp b/src/script/api/script_company.cpp index 717aee5f50..881af1ec05 100644 --- a/src/script/api/script_company.cpp +++ b/src/script/api/script_company.cpp @@ -178,6 +178,8 @@ { company = ResolveCompanyID(company); if (company == COMPANY_INVALID) return -1; + /* If we return INT64_MAX as usual, overflows may occur in the script. So return a smaller value. */ + if (_settings_game.difficulty.infinite_money) return INT32_MAX; return GetAvailableMoney((::CompanyID)company); } diff --git a/src/script/api/script_company.hpp b/src/script/api/script_company.hpp index 762b8ffcdf..bfdc2a70c2 100644 --- a/src/script/api/script_company.hpp +++ b/src/script/api/script_company.hpp @@ -258,9 +258,10 @@ public: /** * Gets the bank balance. In other words, the amount of money the given company can spent. + * If infinite money is enabled, it returns INT32_MAX. * @param company The company to get the bank balance of. * @pre ResolveCompanyID(company) != COMPANY_INVALID. - * @return The actual bank balance. + * @return The actual bank balance or INT32_MAX. */ static Money GetBankBalance(CompanyID company);