SDL_image: IMG_stb: Fix a memory leak for PNG.

From 0dc1f1ce77036020d46aca5372cded9165f54a19 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=AE=8B=E6=96=87=E6=AD=A6?= <[EMAIL REDACTED]>
Date: Sun, 20 Apr 2025 15:25:44 +0800
Subject: [PATCH] IMG_stb: Fix a memory leak for PNG.

---
 src/IMG_stb.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/src/IMG_stb.c b/src/IMG_stb.c
index d38faa803..484886ed2 100644
--- a/src/IMG_stb.c
+++ b/src/IMG_stb.c
@@ -157,6 +157,14 @@ SDL_Surface *IMG_LoadSTB_RW(SDL_RWops *src)
             int colorkey_index = -1;
             SDL_bool has_alpha = SDL_FALSE;
             SDL_Palette *palette = surface->format->palette;
+
+            /* FIXME: This sucks. It'd be better to allocate the surface first, then
+             * write directly to the pixel buffer:
+             * https://github.com/nothings/stb/issues/58
+             * -flibit
+             */
+            surface->flags &= ~SDL_PREALLOC;
+
             if (palette) {
                 int i;
                 Uint8 *palette_bytes = (Uint8 *)palette_colors;
@@ -185,15 +193,6 @@ SDL_Surface *IMG_LoadSTB_RW(SDL_RWops *src)
             } else if (has_colorkey) {
                 SDL_SetColorKey(surface, SDL_TRUE, colorkey_index);
             }
-
-            /* FIXME: This sucks. It'd be better to allocate the surface first, then
-             * write directly to the pixel buffer:
-             * https://github.com/nothings/stb/issues/58
-             * -flibit
-             */
-            if (surface) {
-                surface->flags &= ~SDL_PREALLOC;
-            }
         }
 
     } else if (format == STBI_grey || format == STBI_rgb || format == STBI_rgb_alpha) {