From 833649621a7ada162c017234bec4992553ebda23 Mon Sep 17 00:00:00 2001 From: mcfarlandjb Date: Mon, 17 Feb 2014 02:57:21 +0000 Subject: [PATCH] Fixed random behavior occurring due to passing 0xFFFF into SDL_GameControllerGetButton. May have shown up as unexpected button presses, or something far worse like a segfault. Source comments explain a little better. --- .../sdl2/src/drivers/sdl/sdl-gamectrl.cpp | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/branches/sdl2/src/drivers/sdl/sdl-gamectrl.cpp b/branches/sdl2/src/drivers/sdl/sdl-gamectrl.cpp index df9650a3..96c829f1 100644 --- a/branches/sdl2/src/drivers/sdl/sdl-gamectrl.cpp +++ b/branches/sdl2/src/drivers/sdl/sdl-gamectrl.cpp @@ -51,11 +51,26 @@ DTestButtonGC(ButtConfig *bc) for(x = 0; x < bc->NumC; x++) { gc = s_GameCtrls[bc->DeviceNum[x]]; - if( SDL_GameControllerGetButton(gc, (SDL_GameControllerButton) bc->ButtonNum[x]) ) + /*** + * ButtConfig->ButtonNum is a uint16. SDL_CONTROLLER_BUTTON_INVALID, + * used to mark unused button mappings (since 0 is a valid btn too), is + * defined as -1, but this turns into 0xFFFF, which doesn't cast back to + * SDL_GameControllerButton type. + * + * Without bounds-checking, if 0xFFFF is passed into GetButton, it results + * in random behavior since GetButton lacks internal bounds checking. The + * "fun" symptom that lead to discovery of this issue was that "select" + * would also press the turbo A and turbo B buttons. + */ + if( bc->ButtonNum[x] < SDL_CONTROLLER_BUTTON_MAX ) { - //printf("GC %d btn '%s'=down\n", bc->DeviceNum[x], - // SDL_GameControllerGetStringForButton( (SDL_GameControllerButton) bc->ButtonNum[x])); - return 1; + /** Check to see if this button is depressed */ + if( SDL_GameControllerGetButton(gc, (SDL_GameControllerButton) bc->ButtonNum[x]) ) + { + //printf("GC %d btn '%s'=down\n", bc->DeviceNum[x], + // SDL_GameControllerGetStringForButton( (SDL_GameControllerButton) bc->ButtonNum[x])); + return 1; + } } } return 0;