SDL_image: Relax parsing of ANI files to accomodate legacy/malformed assets

From 67da91cad10896b6967217bf2f924a33eb149b7b Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 1 Jun 2026 22:11:47 -0700
Subject: [PATCH] Relax parsing of ANI files to accomodate legacy/malformed
 assets

Fixes https://github.com/libsdl-org/SDL_image/issues/748
---
 src/IMG_ani.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/IMG_ani.c b/src/IMG_ani.c
index b8d84d6e7..bc705a435 100644
--- a/src/IMG_ani.c
+++ b/src/IMG_ani.c
@@ -417,7 +417,7 @@ bool IMG_CreateANIAnimationDecoder(IMG_AnimationDecoder *decoder, SDL_Properties
         Uint32 size;
         if (!SDL_ReadU32LE(decoder->src, &chunk) ||
             !SDL_ReadU32LE(decoder->src, &size)) {
-            goto done;
+            break;
         }
         offset += 8;
 
@@ -456,6 +456,12 @@ bool IMG_CreateANIAnimationDecoder(IMG_AnimationDecoder *decoder, SDL_Properties
         offset += size;
     }
 
+    // Make sure we have a valid animation
+    if (!parse.has_anih) {
+        SDL_SetError("Incomplete ANI data");
+        goto done;
+    }
+
     decoder->GetNextFrame = IMG_AnimationDecoderGetNextFrame_Internal;
     decoder->Reset = IMG_AnimationDecoderReset_Internal;
     decoder->Close = IMG_AnimationDecoderClose_Internal;