From ee8d7d3abd926d2541b18e3309c6daccc5aea3a9 Mon Sep 17 00:00:00 2001 From: Robert Crossfield Date: Sun, 24 May 2026 12:13:12 +1000 Subject: [PATCH] Clamp Amiga image palette copy size to destination capacity --- Source/Amiga/Graphics_Amiga.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Source/Amiga/Graphics_Amiga.cpp b/Source/Amiga/Graphics_Amiga.cpp index 6bb66364..8fcc0987 100644 --- a/Source/Amiga/Graphics_Amiga.cpp +++ b/Source/Amiga/Graphics_Amiga.cpp @@ -391,8 +391,11 @@ void cGraphics_Amiga::Load_And_Draw_Image(const std::string& pFilename, unsigned Decoded.mDimension.mHeight = 0x101; } - // Load the palette - Decoded.CopyPalette(mPalette, (1LL << Decoded.mPlanes)); + // Load the palette (clamped to avoid overflow/UB from file-controlled plane counts) + const size_t PaletteCapacity = sizeof(mPalette) / sizeof(mPalette[0]); + const size_t PlaneCount = static_cast(Decoded.mPlanes); + const size_t RequestedColors = (PlaneCount < (sizeof(size_t) * 8)) ? (1ULL << PlaneCount) : PaletteCapacity; + Decoded.CopyPalette(mPalette, std::min(RequestedColors, PaletteCapacity)); mBMHD_Current = Decoded.GetHeader();