Fixed bug 4898 - No rumble because of integer overflow in SDL_JoystickRumble
authorSam Lantinga <slouken@libsdl.org>
Mon, 16 Dec 2019 10:20:03 -0800
changeset 13348448528dc13da
parent 13347 99ecd178999f
child 13349 7324ce6b653b
Fixed bug 4898 - No rumble because of integer overflow in SDL_JoystickRumble

meyraud705

On a Dualshock 4 controller using hidapi driver, calling SDL_JoystickRumble with a duration too long (SDL_HAPTIC_INFINITY for example) causes the rumble to stop immediately.

This happens because of integer overflow on line 301 of SDL_hidapi_ps4.c
(https://hg.libsdl.org/SDL/file/99ecd178999f/src/joystick/hidapi/SDL_hidapi_ps4.c#l301), which sets expiration time in the past.
src/joystick/hidapi/SDL_hidapi_ps4.c
src/joystick/hidapi/SDL_hidapi_xbox360.c
src/joystick/hidapi/SDL_hidapi_xboxone.c
src/joystick/hidapi/SDL_hidapijoystick_c.h
     1.1 --- a/src/joystick/hidapi/SDL_hidapi_ps4.c	Thu Dec 12 19:07:26 2019 -0800
     1.2 +++ b/src/joystick/hidapi/SDL_hidapi_ps4.c	Mon Dec 16 10:20:03 2019 -0800
     1.3 @@ -298,7 +298,7 @@
     1.4      }
     1.5  
     1.6      if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
     1.7 -        ctx->rumble_expiration = SDL_GetTicks() + duration_ms;
     1.8 +        ctx->rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
     1.9      } else {
    1.10          ctx->rumble_expiration = 0;
    1.11      }
     2.1 --- a/src/joystick/hidapi/SDL_hidapi_xbox360.c	Thu Dec 12 19:07:26 2019 -0800
     2.2 +++ b/src/joystick/hidapi/SDL_hidapi_xbox360.c	Mon Dec 16 10:20:03 2019 -0800
     2.3 @@ -381,7 +381,7 @@
     2.4  #endif /* __WIN32__ */
     2.5  
     2.6      if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
     2.7 -        ctx->rumble_expiration = SDL_GetTicks() + duration_ms;
     2.8 +        ctx->rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
     2.9      } else {
    2.10          ctx->rumble_expiration = 0;
    2.11      }
     3.1 --- a/src/joystick/hidapi/SDL_hidapi_xboxone.c	Thu Dec 12 19:07:26 2019 -0800
     3.2 +++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c	Mon Dec 16 10:20:03 2019 -0800
     3.3 @@ -298,7 +298,7 @@
     3.4      }
     3.5  
     3.6      if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
     3.7 -        ctx->rumble_expiration = SDL_GetTicks() + duration_ms;
     3.8 +        ctx->rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
     3.9      } else {
    3.10          ctx->rumble_expiration = 0;
    3.11      }
     4.1 --- a/src/joystick/hidapi/SDL_hidapijoystick_c.h	Thu Dec 12 19:07:26 2019 -0800
     4.2 +++ b/src/joystick/hidapi/SDL_hidapijoystick_c.h	Mon Dec 16 10:20:03 2019 -0800
     4.3 @@ -43,6 +43,9 @@
     4.4  #undef SDL_JOYSTICK_HIDAPI_XBOXONE
     4.5  #endif
     4.6  
     4.7 +/* Prevent rumble duration overflow */
     4.8 +#define SDL_MAX_RUMBLE_DURATION_MS  0x0fffffff
     4.9 +
    4.10  typedef struct _SDL_HIDAPI_DeviceDriver
    4.11  {
    4.12      const char *hint;