Commit d691010686550f19405a97ffce84eff7ebf6618c

Sam Lantinga 2023-09-24T17:17:49

Check for NULL before dereferencing newly allocated memory Fixes https://github.com/libsdl-org/SDL/issues/8289 (cherry picked from commit 70a1bc69739016884a22abe6bf86f8a5deba29ef) (cherry picked from commit 27f2df2292cceab64ff25f94efca5ec8bc5f1478)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index 479beb1..4ba32ef 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -1752,11 +1752,11 @@ static SDL_bool LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMap
     /* We temporarily open the device to check how it's configured. Make
        a fake SDL_Joystick object to do so. */
     joystick = (SDL_Joystick *)SDL_calloc(sizeof(*joystick), 1);
-    joystick->magic = &SDL_joystick_magic;
     if (joystick == NULL) {
         SDL_OutOfMemory();
         return SDL_FALSE;
     }
+    joystick->magic = &SDL_joystick_magic;
     SDL_memcpy(&joystick->guid, &item->guid, sizeof(item->guid));
 
     joystick->hwdata = (struct joystick_hwdata *)