From e34e953ebaf16c74e983e03c8c6eb0f4301ba3ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20P=2E=20Tjern=C3=B8?= Date: Tue, 12 Feb 2013 17:07:21 -0800 Subject: [PATCH] Check bounds in SDL_IsGameController. Switch two functions to SDL_bool. Switches SDL_GameControllerGetAttached and SDL_IsGameController to return SDL_bool, instead of int. --- include/SDL_gamecontroller.h | 4 ++-- src/joystick/SDL_gamecontroller.c | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/include/SDL_gamecontroller.h b/include/SDL_gamecontroller.h index d65805ca3..75fb4351e 100644 --- a/include/SDL_gamecontroller.h +++ b/include/SDL_gamecontroller.h @@ -113,7 +113,7 @@ typedef struct _SDL_GameControllerButtonBind * Is the joystick on this index supported by the game controller interface? * returns 1 if supported, 0 otherwise. */ -extern DECLSPEC int SDLCALL SDL_IsGameController(int joystick_index); +extern DECLSPEC SDL_bool SDLCALL SDL_IsGameController(int joystick_index); /** @@ -141,7 +141,7 @@ extern DECLSPEC const char *SDLCALL SDL_GameControllerName(SDL_GameController * /** * Returns 1 if the controller has been opened and currently connected, or 0 if it has not. */ -extern DECLSPEC int SDLCALL SDL_GameControllerGetAttached(SDL_GameController * gamecontroller); +extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerGetAttached(SDL_GameController * gamecontroller); /** * Get the underlying joystick object used by a controller diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c index 66e68a561..944eece87 100644 --- a/src/joystick/SDL_gamecontroller.c +++ b/src/joystick/SDL_gamecontroller.c @@ -265,6 +265,10 @@ int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event) */ ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index) { + if ( (device_index < 0) || (device_index >= SDL_NumJoysticks()) ) { + return NULL; + } + #ifdef SDL_JOYSTICK_DINPUT if ( SDL_SYS_IsXInputDeviceIndex(device_index) && s_pXInputMapping ) { @@ -741,14 +745,16 @@ SDL_GameControllerNameForIndex(int device_index) /* * Return 1 if the joystick at this device index is a supported controller */ -int SDL_IsGameController(int device_index) +SDL_bool +SDL_IsGameController(int device_index) { ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(device_index); if ( pSupportedController ) { - return 1; + return SDL_TRUE; } - return 0; + + return SDL_FALSE; } /* @@ -881,11 +887,11 @@ SDL_GameControllerGetButton(SDL_GameController * gamecontroller, SDL_CONTROLLER_ * Return if the joystick in question is currently attached to the system, * \return 0 if not plugged in, 1 if still present. */ -int +SDL_bool SDL_GameControllerGetAttached( SDL_GameController * gamecontroller ) { if ( !gamecontroller ) - return 0; + return SDL_FALSE; return SDL_JoystickGetAttached(gamecontroller->joystick); }