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>
This commit is contained in:
parent
95658f57fc
commit
296255d492
|
@ -51,12 +51,13 @@ wxSDLJoy::wxSDLJoy(bool analog)
|
||||||
for (int i = 0; i < njoy; i++) {
|
for (int i = 0; i < njoy; i++) {
|
||||||
SDL_Joystick* dev = joystate[i].dev = SDL_JoystickOpen(i);
|
SDL_Joystick* dev = joystate[i].dev = SDL_JoystickOpen(i);
|
||||||
|
|
||||||
int nctrl = 0;
|
int nctrl = 0, axes = 0, hats = 0, buttons = 0;
|
||||||
nctrl += joystate[i].nax = SDL_JoystickNumAxes(dev);
|
|
||||||
nctrl += joystate[i].nhat = SDL_JoystickNumHats(dev);
|
|
||||||
nctrl += joystate[i].nbut = SDL_JoystickNumButtons(dev);
|
|
||||||
|
|
||||||
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;
|
joystate[i].is_valid = false;
|
||||||
|
|
||||||
SDL_JoystickClose(dev);
|
SDL_JoystickClose(dev);
|
||||||
|
@ -65,6 +66,10 @@ wxSDLJoy::wxSDLJoy(bool analog)
|
||||||
continue;
|
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};
|
joystate[i].curval = new short[nctrl]{0};
|
||||||
|
|
||||||
// clear controls
|
// clear controls
|
||||||
|
|
Loading…
Reference in New Issue