Browse Source

Fixed crash if allocating memory for game controller failed.

Philipp Wiesemann 8 years ago
parent
commit
af26379881
1 changed files with 21 additions and 2 deletions
  1. 21 2
      src/joystick/SDL_gamecontroller.c

+ 21 - 2
src/joystick/SDL_gamecontroller.c

@@ -1205,8 +1205,27 @@ SDL_GameControllerOpen(int device_index)
         return NULL;
     }
 
-    gamecontroller->last_match_axis = (SDL_ExtendedGameControllerBind **)SDL_calloc(gamecontroller->joystick->naxes, sizeof(*gamecontroller->last_match_axis));
-    gamecontroller->last_hat_mask = (Uint8 *)SDL_calloc(gamecontroller->joystick->nhats, sizeof(*gamecontroller->last_hat_mask));
+    if (gamecontroller->joystick->naxes) {
+        gamecontroller->last_match_axis = (SDL_ExtendedGameControllerBind **)SDL_calloc(gamecontroller->joystick->naxes, sizeof(*gamecontroller->last_match_axis));
+        if (!gamecontroller->last_match_axis) {
+            SDL_OutOfMemory();
+            SDL_JoystickClose(gamecontroller->joystick);
+            SDL_free(gamecontroller);
+            SDL_UnlockJoystickList();
+            return NULL;
+        }
+    }
+    if (gamecontroller->joystick->nhats) {
+        gamecontroller->last_hat_mask = (Uint8 *)SDL_calloc(gamecontroller->joystick->nhats, sizeof(*gamecontroller->last_hat_mask));
+        if (!gamecontroller->last_hat_mask) {
+            SDL_OutOfMemory();
+            SDL_JoystickClose(gamecontroller->joystick);
+            SDL_free(gamecontroller->last_match_axis);
+            SDL_free(gamecontroller);
+            SDL_UnlockJoystickList();
+            return NULL;
+        }
+    }
 
     SDL_PrivateLoadButtonMapping(gamecontroller, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping);