SDL: WGI_JoystickUpdate: bounds-check array sizes

From ca915b1884f8c38c5e53010703d0567db7f2358a Mon Sep 17 00:00:00 2001
From: Shawn Hoffman <[EMAIL REDACTED]>
Date: Tue, 30 Aug 2022 13:54:58 -0700
Subject: [PATCH] WGI_JoystickUpdate: bounds-check array sizes

---
 .../windows/SDL_windows_gaming_input.c        | 28 +++++++++++++------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/src/joystick/windows/SDL_windows_gaming_input.c b/src/joystick/windows/SDL_windows_gaming_input.c
index a2526149300..5c920d91bc0 100644
--- a/src/joystick/windows/SDL_windows_gaming_input.c
+++ b/src/joystick/windows/SDL_windows_gaming_input.c
@@ -798,26 +798,36 @@ WGI_JoystickUpdate(SDL_Joystick *joystick)
 {
     struct joystick_hwdata *hwdata = joystick->hwdata;
     HRESULT hr;
-    UINT32 nbuttons = joystick->nbuttons;
-    boolean *buttons = SDL_stack_alloc(boolean, nbuttons);
-    UINT32 nhats = joystick->nhats;
-    __x_ABI_CWindows_CGaming_CInput_CGameControllerSwitchPosition *hats = SDL_stack_alloc(__x_ABI_CWindows_CGaming_CInput_CGameControllerSwitchPosition, nhats);
-    UINT32 naxes = joystick->naxes;
-    DOUBLE *axes = SDL_stack_alloc(DOUBLE, naxes);
+    UINT32 nbuttons = SDL_min(joystick->nbuttons, SDL_MAX_UINT8);
+    boolean *buttons = NULL;
+    UINT32 nhats = SDL_min(joystick->nhats, SDL_MAX_UINT8);
+    __x_ABI_CWindows_CGaming_CInput_CGameControllerSwitchPosition *hats = NULL;
+    UINT32 naxes = SDL_min(joystick->naxes, SDL_MAX_UINT8);
+    DOUBLE *axes = NULL;
     UINT64 timestamp;
 
+    if (nbuttons > 0) {
+        buttons = SDL_stack_alloc(boolean, nbuttons);
+    }
+    if (nhats > 0) {
+        hats = SDL_stack_alloc(__x_ABI_CWindows_CGaming_CInput_CGameControllerSwitchPosition, nhats);
+    }
+    if (naxes > 0) {
+        axes = SDL_stack_alloc(DOUBLE, naxes);
+    }
+
     hr = __x_ABI_CWindows_CGaming_CInput_CIRawGameController_GetCurrentReading(hwdata->controller, nbuttons, buttons, nhats, hats, naxes, axes, &timestamp);
     if (SUCCEEDED(hr) && timestamp != hwdata->timestamp) {
         UINT32 i;
 
         for (i = 0; i < nbuttons; ++i) {
-            SDL_PrivateJoystickButton(joystick, i, buttons[i]);
+            SDL_PrivateJoystickButton(joystick, (Uint8)i, buttons[i]);
         }
         for (i = 0; i < nhats; ++i) {
-            SDL_PrivateJoystickHat(joystick, i, ConvertHatValue(hats[i]));
+            SDL_PrivateJoystickHat(joystick, (Uint8)i, ConvertHatValue(hats[i]));
         }
         for (i = 0; i < naxes; ++i) {
-            SDL_PrivateJoystickAxis(joystick, i, (int)(axes[i] * 65535) - 32768);
+            SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)((int) (axes[i] * 65535) - 32768));
         }
         hwdata->timestamp = timestamp;
     }