From 96bfa633fe8a765134c352077975016267831485 Mon Sep 17 00:00:00 2001 From: eaeiv <60052776+eaeiv@users.noreply.github.com> Date: Thu, 9 Feb 2023 16:51:03 -0500 Subject: [PATCH] Widens TotalRideValue to fix overflow reported in #19292 (#19326) * Widens TotalRideValue to 64 bits * Adds name to contributors file * Adds totalRideValue overflow fix to changelog --- contributors.md | 1 + distribution/changelog.txt | 1 + src/openrct2/park/ParkFile.cpp | 11 ++++++++++- src/openrct2/park/ParkFile.h | 4 ++-- src/openrct2/ride/Ride.h | 2 +- src/openrct2/ride/RideConstruction.cpp | 2 +- src/openrct2/scenario/Scenario.cpp | 2 +- src/openrct2/scripting/bindings/world/ScPark.cpp | 2 +- src/openrct2/scripting/bindings/world/ScPark.hpp | 2 +- src/openrct2/world/Park.cpp | 6 +++--- src/openrct2/world/Park.h | 2 +- 11 files changed, 23 insertions(+), 12 deletions(-) diff --git a/contributors.md b/contributors.md index a0e08eba13..0c981c0b15 100644 --- a/contributors.md +++ b/contributors.md @@ -208,6 +208,7 @@ The following people are not part of the development team, but have been contrib * Michael Bickerton (mdbckrtn) * Nehemiah Negussie (nehemiah-negussie) * (zzril) +* Ernest Elgin (eaeiv) ## Toolchain diff --git a/distribution/changelog.txt b/distribution/changelog.txt index 3a9871a497..89085606c5 100644 --- a/distribution/changelog.txt +++ b/distribution/changelog.txt @@ -37,6 +37,7 @@ - Fix: [#19114] [Plugin] GameActionResult does not comply to API specification. - Fix: [#19136] SV6 saves with experimental RCT1 paths not imported correctly. - Fix: [#19250] MusicObjects do not free their preview images +- Fix: [#19292] Overflow in totalRideValue 0.4.3 (2022-12-14) ------------------------------------------------------------------------ diff --git a/src/openrct2/park/ParkFile.cpp b/src/openrct2/park/ParkFile.cpp index 817899bbbc..a2cc5d3156 100644 --- a/src/openrct2/park/ParkFile.cpp +++ b/src/openrct2/park/ParkFile.cpp @@ -828,7 +828,16 @@ namespace OpenRCT2 cs.ReadWrite(gWeeklyProfitAverageDivisor); cs.ReadWrite(gTotalAdmissions); cs.ReadWrite(gTotalIncomeFromAdmissions); - cs.ReadWrite(gTotalRideValueForMoney); + if (version <= 16) + { + money16 legacyTotalRideValueForMoney = 0; + cs.ReadWrite(legacyTotalRideValueForMoney); + gTotalRideValueForMoney = legacyTotalRideValueForMoney; + } + else + { + cs.ReadWrite(gTotalRideValueForMoney); + } cs.ReadWrite(gNumGuestsInParkLastWeek); cs.ReadWrite(gGuestChangeModifier); cs.ReadWrite(_guestGenerationProbability); diff --git a/src/openrct2/park/ParkFile.h b/src/openrct2/park/ParkFile.h index 7df5ab0959..3b5f0054e3 100644 --- a/src/openrct2/park/ParkFile.h +++ b/src/openrct2/park/ParkFile.h @@ -8,10 +8,10 @@ struct ObjectRepositoryItem; namespace OpenRCT2 { // Current version that is saved. - constexpr uint32_t PARK_FILE_CURRENT_VERSION = 16; + constexpr uint32_t PARK_FILE_CURRENT_VERSION = 17; // The minimum version that is forwards compatible with the current version. - constexpr uint32_t PARK_FILE_MIN_VERSION = 14; + constexpr uint32_t PARK_FILE_MIN_VERSION = 17; // The minimum version that is backwards compatible with the current version. // If this is increased beyond 0, uncomment the checks in ParkFile.cpp and Context.cpp! diff --git a/src/openrct2/ride/Ride.h b/src/openrct2/ride/Ride.h index cb5b15148a..fc02a57c5b 100644 --- a/src/openrct2/ride/Ride.h +++ b/src/openrct2/ride/Ride.h @@ -999,7 +999,7 @@ Ride* GetOrAllocateRide(RideId index); const RideObjectEntry* GetRideEntryByIndex(ObjectEntryIndex index); std::string_view GetRideEntryName(ObjectEntryIndex index); -extern money16 gTotalRideValueForMoney; +extern money64 gTotalRideValueForMoney; extern const StringId ColourSchemeNames[4]; diff --git a/src/openrct2/ride/RideConstruction.cpp b/src/openrct2/ride/RideConstruction.cpp index cdffb5d8c2..c9aea7ea26 100644 --- a/src/openrct2/ride/RideConstruction.cpp +++ b/src/openrct2/ride/RideConstruction.cpp @@ -50,7 +50,7 @@ using namespace OpenRCT2::TrackMetaData; -money16 gTotalRideValueForMoney; +money64 gTotalRideValueForMoney; money32 _currentTrackPrice; diff --git a/src/openrct2/scenario/Scenario.cpp b/src/openrct2/scenario/Scenario.cpp index d5d703311c..3e7f914c42 100644 --- a/src/openrct2/scenario/Scenario.cpp +++ b/src/openrct2/scenario/Scenario.cpp @@ -242,7 +242,7 @@ void ScenarioSuccessSubmitName(const char* name) */ static void ScenarioCheckEntranceFeeTooHigh() { - const auto max_fee = AddClamp_money16(gTotalRideValueForMoney, gTotalRideValueForMoney / 2); + const auto max_fee = AddClamp_money64(gTotalRideValueForMoney, gTotalRideValueForMoney / 2); if ((gParkFlags & PARK_FLAGS_PARK_OPEN) && ParkGetEntranceFee() > max_fee) { diff --git a/src/openrct2/scripting/bindings/world/ScPark.cpp b/src/openrct2/scripting/bindings/world/ScPark.cpp index e1fe4bd710..7a7ab950e3 100644 --- a/src/openrct2/scripting/bindings/world/ScPark.cpp +++ b/src/openrct2/scripting/bindings/world/ScPark.cpp @@ -191,7 +191,7 @@ namespace OpenRCT2::Scripting } } - money16 ScPark::totalRideValueForMoney_get() const + money64 ScPark::totalRideValueForMoney_get() const { return gTotalRideValueForMoney; } diff --git a/src/openrct2/scripting/bindings/world/ScPark.hpp b/src/openrct2/scripting/bindings/world/ScPark.hpp index d8fdac8da5..db17323e7d 100644 --- a/src/openrct2/scripting/bindings/world/ScPark.hpp +++ b/src/openrct2/scripting/bindings/world/ScPark.hpp @@ -59,7 +59,7 @@ namespace OpenRCT2::Scripting money64 companyValue_get() const; void companyValue_set(money64 value); - money16 totalRideValueForMoney_get() const; + money64 totalRideValueForMoney_get() const; uint32_t totalAdmissions_get() const; void totalAdmissions_set(uint32_t value); diff --git a/src/openrct2/world/Park.cpp b/src/openrct2/world/Park.cpp index e0e13a4199..fe0af56711 100644 --- a/src/openrct2/world/Park.cpp +++ b/src/openrct2/world/Park.cpp @@ -516,9 +516,9 @@ money64 Park::CalculateCompanyValue() const return result; } -money16 Park::CalculateTotalRideValueForMoney() const +money64 Park::CalculateTotalRideValueForMoney() const { - money16 totalRideValue = 0; + money64 totalRideValue = 0; bool ridePricesUnlocked = ParkRidePricesUnlocked() && !(gParkFlags & PARK_FLAGS_NO_MONEY); for (auto& ride : GetRideManager()) { @@ -532,7 +532,7 @@ money16 Park::CalculateTotalRideValueForMoney() const // Add ride value if (ride.value != RIDE_VALUE_UNDEFINED) { - money16 rideValue = static_cast(ride.value); + money64 rideValue = static_cast(ride.value); if (ridePricesUnlocked) { rideValue -= ride.price[0]; diff --git a/src/openrct2/world/Park.h b/src/openrct2/world/Park.h index f55b1185d1..43d74876f7 100644 --- a/src/openrct2/world/Park.h +++ b/src/openrct2/world/Park.h @@ -81,7 +81,7 @@ namespace OpenRCT2 private: money64 CalculateRideValue(const Ride& ride) const; - money16 CalculateTotalRideValueForMoney() const; + money64 CalculateTotalRideValueForMoney() const; uint32_t CalculateSuggestedMaxGuests() const; uint32_t CalculateGuestGenerationProbability() const;