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. (cherry picked from commit e13b74ccf0b20a20bd4baf1617bdc4f84c13feb7)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index fcbfaa4..d66028d 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -1432,15 +1432,25 @@ static int HIDAPI_JoystickOpen(SDL_Joystick *joystick, int device_index)
return 0;
}
-static int HIDAPI_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
+static SDL_bool HIDAPI_GetJoystickDevice(SDL_Joystick *joystick, SDL_HIDAPI_Device **device)
{
- int result;
-
SDL_AssertJoysticksLocked();
- if (joystick->hwdata) {
- SDL_HIDAPI_Device *device = joystick->hwdata->device;
+ 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;
+
+ 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");
@@ -1452,12 +1462,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");
@@ -1469,12 +1476,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);
}
@@ -1484,12 +1488,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");
@@ -1501,12 +1502,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");
@@ -1518,12 +1516,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");