From b6fe4f175c24d788a5132ea49c2c2596c700c4ac Mon Sep 17 00:00:00 2001 From: Clownacy Date: Mon, 12 Oct 2020 21:46:47 +0100 Subject: [PATCH] Make SDL2 controller backend safer There were some edgecases that would causes crashes --- src/Backends/Controller/SDL2.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Backends/Controller/SDL2.cpp b/src/Backends/Controller/SDL2.cpp index 12182e27..87aea8d1 100644 --- a/src/Backends/Controller/SDL2.cpp +++ b/src/Backends/Controller/SDL2.cpp @@ -43,15 +43,24 @@ bool ControllerBackend_GetJoystickStatus(bool **buttons, unsigned int *button_co int total_sdl_buttons = SDL_JoystickNumButtons(joystick); if (total_sdl_buttons < 0) + { + total_sdl_buttons = 0; Backend_PrintError("Failed to get number of buttons on joystick: %s", SDL_GetError()); + } int total_sdl_axes = SDL_JoystickNumAxes(joystick); if (total_sdl_axes < 0) + { + total_sdl_axes = 0; Backend_PrintError("Failed to get number of general axis controls on joystick: %s", SDL_GetError()); + } int total_sdl_hats = SDL_JoystickNumHats(joystick); if (total_sdl_hats < 0) + { + total_sdl_hats = 0; Backend_PrintError("Failed to get number of POV hats on joystick: %s", SDL_GetError()); + } *button_count = total_sdl_buttons + total_sdl_axes * 2 + total_sdl_hats * 4; *axis_count = total_sdl_axes; @@ -60,12 +69,17 @@ bool ControllerBackend_GetJoystickStatus(bool **buttons, unsigned int *button_co static short *axis_buffer = NULL; bool *new_button_buffer = (bool*)realloc(button_buffer, *button_count * sizeof(bool)); - short *new_axis_buffer = (short*)realloc(axis_buffer, *axis_count * sizeof(short)); - if (new_button_buffer == NULL || new_axis_buffer == NULL) + if (new_button_buffer == NULL) return false; button_buffer = new_button_buffer; + + short *new_axis_buffer = (short*)realloc(axis_buffer, *axis_count * sizeof(short)); + + if (new_axis_buffer == NULL) + return false; + axis_buffer = new_axis_buffer; //////////////////////////