From 0f74591f404d28d4b8a843b7b4d908eea212cabe Mon Sep 17 00:00:00 2001 From: ZehMatt Date: Fri, 30 Jul 2021 01:44:01 +0300 Subject: [PATCH 1/7] Use std::vector<> instead of malloc/free in CursorRepository.cpp --- src/openrct2-ui/CursorRepository.cpp | 34 +++++++++++++--------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/openrct2-ui/CursorRepository.cpp b/src/openrct2-ui/CursorRepository.cpp index 6fab01a596..4c703a1089 100644 --- a/src/openrct2-ui/CursorRepository.cpp +++ b/src/openrct2-ui/CursorRepository.cpp @@ -14,6 +14,7 @@ #include #include #include +#include using namespace OpenRCT2::Ui; @@ -45,19 +46,19 @@ void CursorRepository::SetCurrentCursor(CursorID cursorId) } } -static bool getBit(const uint8_t* data, size_t x, size_t y, size_t width) +static bool getBit(const uint8_t data[], size_t x, size_t y, size_t width) noexcept { - size_t position = y * width + x; + const size_t position = y * width + x; return (data[position / 8] & (1 << (7 - (x % 8)))) != 0; } -static void setBit(uint8_t* data, size_t x, size_t y, size_t width) +static void setBit(uint8_t data[], size_t x, size_t y, size_t width) noexcept { size_t position = y * width + x; data[position / 8] |= (1 << (7 - (position % 8))); } -static void drawRect(uint8_t* data, size_t x, size_t y, size_t width, size_t scale) +static void drawRect(uint8_t data[], size_t x, size_t y, size_t width, size_t scale) noexcept { for (size_t outY = (y * scale); outY < ((1 + y) * scale); outY++) { @@ -68,42 +69,39 @@ static void drawRect(uint8_t* data, size_t x, size_t y, size_t width, size_t sca } } -static uint8_t* scaleDataArray(const uint8_t data[], size_t width, size_t height, size_t scale) +static std::vector scaleDataArray(const uint8_t data[], size_t width, size_t height, size_t scale) { - size_t length = width * height; - auto* ret = static_cast(calloc(sizeof(uint8_t), length * scale * scale)); + const size_t length = width * height; + + std::vector res; + res.resize(length * scale * scale); for (size_t y = 0; y < height * 8; y++) { for (size_t x = 0; x < width; x++) { - bool value = getBit(data, x, y, width); + const bool value = getBit(data, x, y, width); if (!value) continue; - drawRect(ret, x, y, width, scale); + drawRect(res.data(), x, y, width, scale); } } - return ret; + return res; } SDL_Cursor* CursorRepository::Create(const CursorData* cursorInfo, uint8_t scale) { - SDL_Cursor* cursor; - - auto integer_scale = static_cast(round(scale)); + const auto integer_scale = static_cast(round(scale)); auto data = scaleDataArray(cursorInfo->Data, CURSOR_BIT_WIDTH, CURSOR_HEIGHT, static_cast(integer_scale)); auto mask = scaleDataArray(cursorInfo->Mask, CURSOR_BIT_WIDTH, CURSOR_HEIGHT, static_cast(integer_scale)); - cursor = SDL_CreateCursor( - data, mask, BASE_CURSOR_WIDTH * integer_scale, BASE_CURSOR_HEIGHT * integer_scale, + auto* cursor = SDL_CreateCursor( + data.data(), mask.data(), BASE_CURSOR_WIDTH * integer_scale, BASE_CURSOR_HEIGHT * integer_scale, cursorInfo->HotSpot.X * integer_scale, cursorInfo->HotSpot.Y * integer_scale); - free(data); - free(mask); - return cursor; } From ee129db4e07ae2b28735bdb3d6c05ef6ea248b3b Mon Sep 17 00:00:00 2001 From: ZehMatt Date: Fri, 30 Jul 2021 02:14:23 +0300 Subject: [PATCH 2/7] Move static initialization out and fix bounds check --- src/openrct2/peep/Peep.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/openrct2/peep/Peep.cpp b/src/openrct2/peep/Peep.cpp index 6e7fff074e..36f9f1e6a8 100644 --- a/src/openrct2/peep/Peep.cpp +++ b/src/openrct2/peep/Peep.cpp @@ -1460,6 +1460,13 @@ void Peep::FormatActionTo(Formatter& ft) const } } +static constexpr const rct_string_id _staffNames[] = { + STR_HANDYMAN_X, + STR_MECHANIC_X, + STR_SECURITY_GUARD_X, + STR_ENTERTAINER_X, +}; + void Peep::FormatNameTo(Formatter& ft) const { if (Name == nullptr) @@ -1467,20 +1474,13 @@ void Peep::FormatNameTo(Formatter& ft) const auto* staff = As(); if (staff != nullptr) { - static constexpr const rct_string_id staffNames[] = { - STR_HANDYMAN_X, - STR_MECHANIC_X, - STR_SECURITY_GUARD_X, - STR_ENTERTAINER_X, - }; - auto staffNameIndex = static_cast(staff->AssignedStaffType); - if (staffNameIndex > sizeof(staffNames)) + if (staffNameIndex >= std::size(_staffNames)) { staffNameIndex = 0; } - ft.Add(staffNames[staffNameIndex]); + ft.Add(_staffNames[staffNameIndex]); ft.Add(Id); } else if (gParkFlags & PARK_FLAGS_SHOW_REAL_GUEST_NAMES) From b992951bb22059002b0d5ca15d80894471abc843 Mon Sep 17 00:00:00 2001 From: ZehMatt Date: Fri, 30 Jul 2021 02:17:20 +0300 Subject: [PATCH 3/7] Default initialize SortOrder in ListViewColumn --- src/openrct2-ui/scripting/CustomListView.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openrct2-ui/scripting/CustomListView.h b/src/openrct2-ui/scripting/CustomListView.h index 093148cd58..43eb9edb4d 100644 --- a/src/openrct2-ui/scripting/CustomListView.h +++ b/src/openrct2-ui/scripting/CustomListView.h @@ -41,7 +41,7 @@ namespace OpenRCT2::Ui::Windows struct ListViewColumn { bool CanSort{}; - ColumnSortOrder SortOrder; + ColumnSortOrder SortOrder{}; std::string Header; std::string HeaderTooltip; std::optional RatioWidth{}; From aeb68008fae6b6b33c71f34574a47b4dd281d1a5 Mon Sep 17 00:00:00 2001 From: ZehMatt Date: Fri, 30 Jul 2021 02:19:18 +0300 Subject: [PATCH 4/7] Fix uninitialized variable in Http.h --- src/openrct2/core/Http.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/openrct2/core/Http.h b/src/openrct2/core/Http.h index 873d9b7e5e..e593518d3d 100644 --- a/src/openrct2/core/Http.h +++ b/src/openrct2/core/Http.h @@ -22,6 +22,7 @@ namespace Http { enum class Status { + Invalid = 0, Ok = 200, NotFound = 404 }; @@ -35,20 +36,20 @@ namespace Http struct Response { - Status status; + Status status{}; std::string content_type; - std::string body = ""; + std::string body; std::map header = {}; - std::string error = ""; + std::string error; }; struct Request { std::string url; - std::map header = {}; + std::map header; Method method = Method::GET; - std::string body = ""; - bool forceIPv4 = false; + std::string body; + bool forceIPv4{}; }; Response Do(const Request& req); @@ -56,7 +57,7 @@ namespace Http inline void DoAsync(const Request& req, std::function fn) { auto thread = std::thread([=]() { - Response res; + Response res{}; try { res = Do(req); From b111e104e3df6976419e4458600fd5a7892a8714 Mon Sep 17 00:00:00 2001 From: ZehMatt Date: Fri, 30 Jul 2021 02:21:31 +0300 Subject: [PATCH 5/7] Add missing return statement for the nullptr path --- src/openrct2-ui/windows/Sign.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/openrct2-ui/windows/Sign.cpp b/src/openrct2-ui/windows/Sign.cpp index 28836b9741..f6efda0d8c 100644 --- a/src/openrct2-ui/windows/Sign.cpp +++ b/src/openrct2-ui/windows/Sign.cpp @@ -156,6 +156,7 @@ public: if (tileElement == nullptr) { Close(); + return; } auto bannerCoords = banner->position.ToCoordsXY(); From fbc689818e6b8e2e306a50a2d237b822a75a4b08 Mon Sep 17 00:00:00 2001 From: ZehMatt Date: Fri, 30 Jul 2021 02:24:35 +0300 Subject: [PATCH 6/7] Default initialize ErrorMessageArgs in GameAction Result --- src/openrct2/actions/GameAction.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openrct2/actions/GameAction.h b/src/openrct2/actions/GameAction.h index 04ac4b2b36..856470f356 100644 --- a/src/openrct2/actions/GameAction.h +++ b/src/openrct2/actions/GameAction.h @@ -126,7 +126,7 @@ namespace GameActions GameActions::Status Error = GameActions::Status::Ok; StringVariant ErrorTitle; StringVariant ErrorMessage; - std::array ErrorMessageArgs; + std::array ErrorMessageArgs{}; CoordsXYZ Position = { LOCATION_NULL, LOCATION_NULL, LOCATION_NULL }; money32 Cost = 0; ExpenditureType Expenditure = ExpenditureType::Count; From 2a3b25af9aaeb9c0937e418a8d955a519dfe5dc6 Mon Sep 17 00:00:00 2001 From: ZehMatt Date: Fri, 30 Jul 2021 02:52:41 +0300 Subject: [PATCH 7/7] Mark values constexpr in FileAudioSource --- src/openrct2-ui/audio/FileAudioSource.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/openrct2-ui/audio/FileAudioSource.cpp b/src/openrct2-ui/audio/FileAudioSource.cpp index 60f4087b25..b26257fc64 100644 --- a/src/openrct2-ui/audio/FileAudioSource.cpp +++ b/src/openrct2-ui/audio/FileAudioSource.cpp @@ -68,11 +68,11 @@ namespace OpenRCT2::Audio bool LoadWAV(SDL_RWops* rw) { - const uint32_t DATA = 0x61746164; - const uint32_t FMT = 0x20746D66; - const uint32_t RIFF = 0x46464952; - const uint32_t WAVE = 0x45564157; - const uint16_t pcmformat = 0x0001; + constexpr uint32_t DATA = 0x61746164; + constexpr uint32_t FMT = 0x20746D66; + constexpr uint32_t RIFF = 0x46464952; + constexpr uint32_t WAVE = 0x45564157; + constexpr uint16_t pcmformat = 0x0001; Unload(); @@ -152,10 +152,10 @@ namespace OpenRCT2::Audio { return subchunkSize; } - const uint32_t FACT = 0x74636166; - const uint32_t LIST = 0x5453494c; - const uint32_t BEXT = 0x74786562; - const uint32_t JUNK = 0x4B4E554A; + constexpr uint32_t FACT = 0x74636166; + constexpr uint32_t LIST = 0x5453494c; + constexpr uint32_t BEXT = 0x74786562; + constexpr uint32_t JUNK = 0x4B4E554A; while (subchunkId == FACT || subchunkId == LIST || subchunkId == BEXT || subchunkId == JUNK) { SDL_RWseek(rw, subchunkSize, RW_SEEK_CUR);