From abc3ed3685eebd82a507251522b6fbf97fa16e53 Mon Sep 17 00:00:00 2001 From: scawful Date: Sat, 17 Aug 2024 12:16:55 -0400 Subject: [PATCH] chore: Add error handling and precondition checks to ApplyPaletteWithTransparent method --- src/app/gfx/bitmap.cc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/app/gfx/bitmap.cc b/src/app/gfx/bitmap.cc index aa9a65a6..9ffb8aaf 100644 --- a/src/app/gfx/bitmap.cc +++ b/src/app/gfx/bitmap.cc @@ -399,6 +399,23 @@ absl::Status Bitmap::ApplyPaletteFromPaletteGroup(const SnesPalette &palette, absl::Status Bitmap::ApplyPaletteWithTransparent(const SnesPalette &palette, int index, int length) { + if (index < 0 || index >= palette.size()) { + return absl::InvalidArgumentError("Invalid palette index"); + } + + if (length < 0 || length > palette.size()) { + return absl::InvalidArgumentError("Invalid palette length"); + } + + if (index + length > palette.size()) { + return absl::InvalidArgumentError("Palette index + length exceeds size"); + } + + if (surface_ == nullptr) { + return absl::FailedPreconditionError( + "Surface is null. Palette not applied"); + } + auto start_index = index * 7; palette_ = palette.sub_palette(start_index, start_index + 7); std::vector colors;