SDL: Fixed warning C6386: Buffer overrun while writing to 'palette_saved_alpha': the writable size is...

From 54dc73aa889805978541705017aa416f04ba45c3 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 4 Dec 2023 20:57:27 -0800
Subject: [PATCH] Fixed warning C6386: Buffer overrun while writing to
 'palette_saved_alpha':  the writable size is
 'sizeof(Uint8)*((palette_saved_alpha_ncolors))' bytes, but '2' bytes might be
 written.

---
 src/video/SDL_surface.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/video/SDL_surface.c b/src/video/SDL_surface.c
index b59df0c51eb4..7b26b5e2eb41 100644
--- a/src/video/SDL_surface.c
+++ b/src/video/SDL_surface.c
@@ -1210,10 +1210,12 @@ SDL_Surface *SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *for
         if (set_opaque) {
             int i;
             palette_saved_alpha_ncolors = surface->format->palette->ncolors;
-            palette_saved_alpha = SDL_stack_alloc(Uint8, palette_saved_alpha_ncolors);
-            for (i = 0; i < palette_saved_alpha_ncolors; i++) {
-                palette_saved_alpha[i] = surface->format->palette->colors[i].a;
-                surface->format->palette->colors[i].a = SDL_ALPHA_OPAQUE;
+            if (palette_saved_alpha_ncolors > 0) {
+                palette_saved_alpha = SDL_stack_alloc(Uint8, palette_saved_alpha_ncolors);
+                for (i = 0; i < palette_saved_alpha_ncolors; i++) {
+                    palette_saved_alpha[i] = surface->format->palette->colors[i].a;
+                    surface->format->palette->colors[i].a = SDL_ALPHA_OPAQUE;
+                }
             }
         }
     }