From fee2469c658a7cfee2cc4c0a2af9115b4f1fcbd3 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 19 Dec 2017 10:48:29 -0800 Subject: [PATCH] Fixed bug 4005 - Android, SDL_IsGameController() crashes is index is out of range Sylvain On Android, if you give an invalid index to SDL_IsGameController(), it will crash in SDL_SYS_IsDPAD_DeviceIndex(). --- src/joystick/SDL_gamecontroller.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c index d976c46b88a21..e286af41ca060 100644 --- a/src/joystick/SDL_gamecontroller.c +++ b/src/joystick/SDL_gamecontroller.c @@ -911,6 +911,13 @@ static ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index) ControllerMapping_t *mapping; SDL_LockJoysticks(); + + if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) { + SDL_SetError("There are %d joysticks available", SDL_NumJoysticks()); + SDL_UnlockJoysticks(); + return (NULL); + } + name = SDL_JoystickNameForIndex(device_index); guid = SDL_JoystickGetDeviceGUID(device_index); mapping = SDL_PrivateGetControllerMappingForNameAndGUID(name, guid); @@ -1353,13 +1360,14 @@ SDL_GameControllerOpen(int device_index) SDL_GameController *gamecontrollerlist; ControllerMapping_t *pSupportedController = NULL; + SDL_LockJoysticks(); + if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) { SDL_SetError("There are %d joysticks available", SDL_NumJoysticks()); + SDL_UnlockJoysticks(); return (NULL); } - SDL_LockJoysticks(); - gamecontrollerlist = SDL_gamecontrollers; /* If the controller is already open, return it */ while (gamecontrollerlist) {