SDL_image: Fixed potential out of bounds read in the LBM loader (thanks @Haind03!)

From 5cfbe85f3a783417eec4d5aa0660b40c2cff5292 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 24 Oct 2026 08:33:25 -0700
Subject: [PATCH] Fixed potential out of bounds read in the LBM loader (thanks
 @Haind03!)

Fixes https://github.com/libsdl-org/SDL_image/issues/769
---
 src/IMG_lbm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/IMG_lbm.c b/src/IMG_lbm.c
index 8c8b73456..96fcadeb9 100644
--- a/src/IMG_lbm.c
+++ b/src/IMG_lbm.c
@@ -236,8 +236,10 @@ SDL_Surface *IMG_LoadLBM_IO(SDL_IOStream *src )
     nbplanes = bmhd.planes;
 
     /* Sanity check: nbplanes must not exceed 8 for paletted images.
-       Higher values cause 1<<nbplanes to exceed the 256-entry palette. */
-    if ( !pbm && nbplanes > 8 && nbplanes != 24 && flagHAM == 0 ) {
+       Higher values cause 1<<nbplanes to exceed the 256-entry palette.
+       For HAM, only 6 (HAM6) and 8 (HAM8) are valid. */
+    const bool validHAM = (flagHAM && (nbplanes == 6 || nbplanes == 8));
+    if ( !pbm && nbplanes > 8 && nbplanes != 24 && !validHAM ) {
         SDL_SetError("LBM: invalid number of bitplanes (%u)", nbplanes);
         goto done;
     }