Skip to content
Permalink
Browse files

handle SDL joystick query errors #451

SDL APIs like `SDL_JoystickNumAxes(dev)` can return a negative error
code. Handle this case better.

This should fix the fatal exception from trying to allocate an array
with an illegal size (negative, due to the error.)

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
  • Loading branch information
rkitover committed Jul 11, 2019
1 parent 95658f5 commit 296255d4926eee601c79edba68437b1a4ca84a70
Showing with 10 additions and 5 deletions.
  1. +10 −5 src/wx/widgets/sdljoy.cpp
@@ -51,12 +51,13 @@ wxSDLJoy::wxSDLJoy(bool analog)
for (int i = 0; i < njoy; i++) {
SDL_Joystick* dev = joystate[i].dev = SDL_JoystickOpen(i);

int nctrl = 0;
nctrl += joystate[i].nax = SDL_JoystickNumAxes(dev);
nctrl += joystate[i].nhat = SDL_JoystickNumHats(dev);
nctrl += joystate[i].nbut = SDL_JoystickNumButtons(dev);
int nctrl = 0, axes = 0, hats = 0, buttons = 0;

if (!nctrl) {
axes = SDL_JoystickNumAxes(dev);
hats = SDL_JoystickNumHats(dev);
buttons = SDL_JoystickNumButtons(dev);

if (buttons <= 0 && axes <= 0 && hats <= 0) {
joystate[i].is_valid = false;

SDL_JoystickClose(dev);
@@ -65,6 +66,10 @@ wxSDLJoy::wxSDLJoy(bool analog)
continue;
}

nctrl += joystate[i].nax = axes < 0 ? 0 : axes;
nctrl += joystate[i].nhat = hats < 0 ? 0 : hats;
nctrl += joystate[i].nbut = buttons < 0 ? 0 : buttons;

joystate[i].curval = new short[nctrl]{0};

// clear controls

0 comments on commit 296255d

Please sign in to comment.
You can’t perform that action at this time.