From a1a2f9b9f85145179c7f763be412d7bfb9e467be Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 12 Jun 2019 10:32:36 -0700 Subject: [PATCH] Fixed bug 4486 - Segfault when pressing a trigger on the Steam Controller (Linux) Matteo Beniamino Pressing a trigger button on a Steam Controller causes a segmentation fault both with stable version and latest mercurial head on Linux. I'm using the recent hid_steam kernel module with lizard_mode disabled (that is no keyboard/mouse emulation). I suspect this is what's happening: the driver exposes two hats. The two hats have indices 0 and 2. Inside linux/SDL_sysjoystick.c two hats are allocated in allocate_hatdata for joystick->hwdata->hats. In HandleHat function the hat parameter (that can be 2) is directly used as the index of the array that only has two elements, causing an out of bounds access. SDL is not expecting to have "holes" between hats indices. The index 2 is calculated in HandleInputEvents() as (ABS_HAT2X - ABS_HAT0X) / 2 where ABS_HAT2X is the value associated to the hat inside the hid_steam module. --- src/joystick/linux/SDL_sysjoystick.c | 7 ++++--- src/joystick/linux/SDL_sysjoystick_c.h | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c index 0309f8c3e54e9..a30fe27d8a6d5 100644 --- a/src/joystick/linux/SDL_sysjoystick.c +++ b/src/joystick/linux/SDL_sysjoystick.c @@ -609,17 +609,18 @@ ConfigJoystick(SDL_Joystick * joystick, int fd) for (i = ABS_HAT0X; i <= ABS_HAT3Y; i += 2) { if (test_bit(i, absbit) || test_bit(i + 1, absbit)) { struct input_absinfo absinfo; + int hat_index = (i - ABS_HAT0X) / 2; if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0) { continue; } #ifdef DEBUG_INPUT_EVENTS - printf("Joystick has hat %d\n", (i - ABS_HAT0X) / 2); + printf("Joystick has hat %d\n", hat_index); printf("Values = { %d, %d, %d, %d, %d }\n", absinfo.value, absinfo.minimum, absinfo.maximum, absinfo.fuzz, absinfo.flat); #endif /* DEBUG_INPUT_EVENTS */ - ++joystick->nhats; + joystick->hwdata->hats_indices[joystick->nhats++] = hat_index; } } if (test_bit(REL_X, relbit) || test_bit(REL_Y, relbit)) { @@ -762,7 +763,7 @@ HandleHat(SDL_Joystick * stick, Uint8 hat, int axis, int value) {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN} }; - the_hat = &stick->hwdata->hats[hat]; + the_hat = &stick->hwdata->hats[stick->hwdata->hats_indices[hat]]; if (value < 0) { value = 0; } else if (value == 0) { diff --git a/src/joystick/linux/SDL_sysjoystick_c.h b/src/joystick/linux/SDL_sysjoystick_c.h index 3ebdc924c1991..ef50730a89fbd 100644 --- a/src/joystick/linux/SDL_sysjoystick_c.h +++ b/src/joystick/linux/SDL_sysjoystick_c.h @@ -62,6 +62,8 @@ struct joystick_hwdata /* Steam Controller support */ SDL_bool m_bSteamController; + /* 4 = (ABS_HAT3X-ABS_HAT0X)/2 (see input-event-codes.h in kernel) */ + int hats_indices[4]; }; #endif /* SDL_sysjoystick_c_h_ */