From 8ebb607fc4692deefdf5108538990c49a5cc06b6 Mon Sep 17 00:00:00 2001 From: Basssiiie Date: Sun, 16 Aug 2026 12:58:51 +0200 Subject: [PATCH] Fix #21632: Crash when loading custom image larger than 300 by 300 pixels (#26934) Fixes #21632: a few crashes that can occur when loading a custom image via plugins. - Catch `std::exception` instead of `std::runtime_error` because the 300x300 check throws a [`std::invalid_argument`](https://github.com/OpenRCT2/OpenRCT2/blob/11da390704950cd30f1275c392140480908183a0/src/openrct2/drawing/ImageImporter.cpp#L34) exception and `std::exception` is the base class of both (`std::runtime_error` is not). - Replaced a few asserts with `std::runtime_error`s as well, because the test script supplied in the issue was tripping over those as well. Now instead of crashing the game or throwing an assert, they log nice readable errors to the console for plugin developers. --- distribution/changelog.txt | 1 + src/openrct2-ui/scripting/ScImageManager.hpp | 2 +- src/openrct2/core/Imaging.cpp | 15 ++++++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/distribution/changelog.txt b/distribution/changelog.txt index a13d8f6700..eccac222c8 100644 --- a/distribution/changelog.txt +++ b/distribution/changelog.txt @@ -6,6 +6,7 @@ - Improved: [#26099] The ride visiblity cheat has been moved into the ride colour/appearance tab. - Improved: [#26862] Add a Liquid Glass app icon for macOS 26 and later. - Improved: [#26936] Rain is now drawn next to the top toolbar, but only when it is centred. +- Fix: [#21632] [Plugin] Crash when loading custom image larger than 300 by 300 pixels. - Fix: [#25169] The convert command strips custom objects from the resulting park. - Fix: [#26811] Crash when a ride points to a non-existing station object. - Fix: [#26816] Water rides ignore zero clearances, preventing adjacent terrain modifications and building them anywhere. diff --git a/src/openrct2-ui/scripting/ScImageManager.hpp b/src/openrct2-ui/scripting/ScImageManager.hpp index 82bb29764c..b8e7d05b71 100644 --- a/src/openrct2-ui/scripting/ScImageManager.hpp +++ b/src/openrct2-ui/scripting/ScImageManager.hpp @@ -156,7 +156,7 @@ namespace OpenRCT2::Scripting { JSSetPixelData(ctx, id, pixelData); } - catch (const std::runtime_error& e) + catch (const std::exception& e) { JS_ThrowInternalError(ctx, "%s", e.what()); return JS_EXCEPTION; diff --git a/src/openrct2/core/Imaging.cpp b/src/openrct2/core/Imaging.cpp index 9625937e87..5a9873ef7c 100644 --- a/src/openrct2/core/Imaging.cpp +++ b/src/openrct2/core/Imaging.cpp @@ -115,7 +115,10 @@ namespace OpenRCT2::Imaging if (colourType == PNG_COLOR_TYPE_RGB) { // 24-bit PNG (no alpha) - Guard::Assert(rowBytes == pngWidth * 3, GUARD_LINE); + if (rowBytes != pngWidth * 3) + { + throw std::runtime_error("PNG must have 24-bit colours in RGB mode. "); + } for (png_uint_32 i = 0; i < pngHeight; i++) { auto src = rowPointers[i]; @@ -131,7 +134,10 @@ namespace OpenRCT2::Imaging else if (bitDepth == 8 && !expandTo32) { // 8-bit paletted or greyscale - Guard::Assert(rowBytes == pngWidth, GUARD_LINE); + if (rowBytes != pngWidth) + { + throw std::runtime_error("PNG must be 8-bit paletted when using \"keep\" palette. "); + } for (png_uint_32 i = 0; i < pngHeight; i++) { std::copy_n(rowPointers[i], rowBytes, dst); @@ -141,7 +147,10 @@ namespace OpenRCT2::Imaging else { // 32-bit PNG (with alpha) - Guard::Assert(rowBytes == pngWidth * 4, GUARD_LINE); + if (rowBytes != pngWidth * 4) + { + throw std::runtime_error("PNG must either have 8-bit paletted, or 24-bit/32-bit full colour data. "); + } for (png_uint_32 i = 0; i < pngHeight; i++) { std::copy_n(rowPointers[i], rowBytes, dst);