Fixed crash if joystick->hwdata != NULL && device->driver == NULL

This should never happen, but we're seeing it in the wild, so make sure that we can never call into a NULL device driver.
This commit is contained in:
Sam Lantinga 2023-04-05 11:23:28 -07:00
parent a21ee02095
commit e13b74ccf0
1 changed files with 25 additions and 30 deletions

View File

@ -1426,15 +1426,25 @@ static int HIDAPI_JoystickOpen(SDL_Joystick *joystick, int device_index)
return 0;
}
static SDL_bool HIDAPI_GetJoystickDevice(SDL_Joystick *joystick, SDL_HIDAPI_Device **device)
{
SDL_AssertJoysticksLocked();
if (joystick && joystick->hwdata) {
*device = joystick->hwdata->device;
if (*device && (*device)->driver) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
static int HIDAPI_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
{
int result;
SDL_HIDAPI_Device *device = NULL;
SDL_AssertJoysticksLocked();
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
if (HIDAPI_GetJoystickDevice(joystick, &device)) {
result = device->driver->RumbleJoystick(device, joystick, low_frequency_rumble, high_frequency_rumble);
} else {
result = SDL_SetError("Rumble failed, device disconnected");
@ -1446,12 +1456,9 @@ static int HIDAPI_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_ru
static int HIDAPI_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
{
int result;
SDL_HIDAPI_Device *device = NULL;
SDL_AssertJoysticksLocked();
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
if (HIDAPI_GetJoystickDevice(joystick, &device)) {
result = device->driver->RumbleJoystickTriggers(device, joystick, left_rumble, right_rumble);
} else {
result = SDL_SetError("Rumble failed, device disconnected");
@ -1463,12 +1470,9 @@ static int HIDAPI_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rum
static Uint32 HIDAPI_JoystickGetCapabilities(SDL_Joystick *joystick)
{
Uint32 result = 0;
SDL_HIDAPI_Device *device = NULL;
SDL_AssertJoysticksLocked();
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
if (HIDAPI_GetJoystickDevice(joystick, &device)) {
result = device->driver->GetJoystickCapabilities(device, joystick);
}
@ -1478,12 +1482,9 @@ static Uint32 HIDAPI_JoystickGetCapabilities(SDL_Joystick *joystick)
static int HIDAPI_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
int result;
SDL_HIDAPI_Device *device = NULL;
SDL_AssertJoysticksLocked();
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
if (HIDAPI_GetJoystickDevice(joystick, &device)) {
result = device->driver->SetJoystickLED(device, joystick, red, green, blue);
} else {
result = SDL_SetError("SetLED failed, device disconnected");
@ -1495,12 +1496,9 @@ static int HIDAPI_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green,
static int HIDAPI_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
int result;
SDL_HIDAPI_Device *device = NULL;
SDL_AssertJoysticksLocked();
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
if (HIDAPI_GetJoystickDevice(joystick, &device)) {
result = device->driver->SendJoystickEffect(device, joystick, data, size);
} else {
result = SDL_SetError("SendEffect failed, device disconnected");
@ -1512,12 +1510,9 @@ static int HIDAPI_JoystickSendEffect(SDL_Joystick *joystick, const void *data, i
static int HIDAPI_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{
int result;
SDL_HIDAPI_Device *device = NULL;
SDL_AssertJoysticksLocked();
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
if (HIDAPI_GetJoystickDevice(joystick, &device)) {
result = device->driver->SetJoystickSensorsEnabled(device, joystick, enabled);
} else {
result = SDL_SetError("SetSensorsEnabled failed, device disconnected");