From 6d09ee97c2c2597f7d6bbc8c176b4952f98b1d41 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sat, 27 Jul 2013 02:45:26 -0700 Subject: [PATCH] Improved error checking in the haptic system, preventing crashes in some cases. Edgar Simo 2012-05-06 02:33:39 EDT I recall that driver being buggy back in the day, but looking over the code there's a number of things being done wrong which I've fixed and it should now properly error out instead of crashing. Also make sure you initialize the haptic subsystem before using haptic commands (which I now more explicitly try to enforce). --- src/haptic/SDL_haptic.c | 44 +++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/haptic/SDL_haptic.c b/src/haptic/SDL_haptic.c index d0b6bd25d..c1b8d74bb 100644 --- a/src/haptic/SDL_haptic.c +++ b/src/haptic/SDL_haptic.c @@ -149,18 +149,23 @@ SDL_HapticOpen(int device_index) return NULL; } + /* Add haptic to list */ + for (i = 0; SDL_haptics[i]; i++) + /* Skip to next haptic */ ; + if (i >= SDL_numhaptics) { + SDL_free(haptic); + SDL_SetError("Haptic: Trying to add device past the number originally detected"); + return NULL; + } + SDL_haptics[i] = haptic; + ++haptic->ref_count; + /* Disable autocenter and set gain to max. */ if (haptic->supported & SDL_HAPTIC_GAIN) SDL_HapticSetGain(haptic, 100); if (haptic->supported & SDL_HAPTIC_AUTOCENTER) SDL_HapticSetAutocenter(haptic, 0); - /* Add haptic to list */ - ++haptic->ref_count; - for (i = 0; SDL_haptics[i]; i++) - /* Skip to next haptic */ ; - SDL_haptics[i] = haptic; - return haptic; } @@ -173,6 +178,13 @@ SDL_HapticOpened(int device_index) { int i, opened; + /* Make sure it's valid. */ + if ((device_index < 0) || (device_index >= SDL_numhaptics)) { + SDL_SetError("Haptic: There are %d haptic devices available", + SDL_numhaptics); + return -1; + } + opened = 0; for (i = 0; SDL_haptics[i]; i++) { if (SDL_haptics[i]->index == (Uint8) device_index) { @@ -262,6 +274,13 @@ SDL_HapticOpenFromJoystick(SDL_Joystick * joystick) int i; SDL_Haptic *haptic; + /* Make sure there is room. */ + if (SDL_numhaptics <= 0) { + SDL_SetError("Haptic: There are %d haptic devices available", + SDL_numhaptics); + return NULL; + } + /* Must be a valid joystick */ if (!SDL_PrivateJoystickValid(joystick)) { SDL_SetError("Haptic: Joystick isn't valid."); @@ -299,10 +318,15 @@ SDL_HapticOpenFromJoystick(SDL_Joystick * joystick) } /* Add haptic to list */ - ++haptic->ref_count; for (i = 0; SDL_haptics[i]; i++) /* Skip to next haptic */ ; + if (i >= SDL_numhaptics) { + SDL_free(haptic); + SDL_SetError("Haptic: Trying to add device past the number originally detected"); + return NULL; + } SDL_haptics[i] = haptic; + ++haptic->ref_count; return haptic; } @@ -757,6 +781,7 @@ SDL_HapticRumbleInit(SDL_Haptic * haptic) int SDL_HapticRumblePlay(SDL_Haptic * haptic, float strength, Uint32 length) { + int ret; SDL_HapticPeriodic *efx; if (!ValidHaptic(haptic)) { @@ -779,7 +804,10 @@ SDL_HapticRumblePlay(SDL_Haptic * haptic, float strength, Uint32 length) efx = &haptic->rumble_effect.periodic; efx->magnitude = (Sint16)(32767.0f*strength); efx->length = length; - SDL_HapticUpdateEffect(haptic, haptic->rumble_id, &haptic->rumble_effect); + ret = SDL_HapticUpdateEffect(haptic, haptic->rumble_id, &haptic->rumble_effect); + if (ret) { + return ret; + } return SDL_HapticRunEffect(haptic, haptic->rumble_id, 1); }