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); }