Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed bug 4486 - Segfault when pressing a trigger on the Steam Contro…
…ller (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.
  • Loading branch information
slouken committed Jun 12, 2019
1 parent d01150a commit a1a2f9b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/joystick/linux/SDL_sysjoystick.c
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/joystick/linux/SDL_sysjoystick_c.h
Expand Up @@ -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_ */
Expand Down

0 comments on commit a1a2f9b

Please sign in to comment.