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:
Rafael Kitover 2019-07-11 21:25:51 +00:00
parent 95658f57fc
commit 296255d492
No known key found for this signature in database
GPG Key ID: 08AB596679D86240
1 changed files with 10 additions and 5 deletions

View File

@ -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