Cocoa: Fix segfault querying joystick elements

It is reportedly possible for IOHIDDeviceCopyMatchingElements to return
NULL on macOS 13 if the application lacks input monitoring permissions.

This commit only prevents the segfault.  More work will be needed to
correctly handle this situation, including Game Controller support.

Related to #2320
Closes #2321

(cherry picked from commit 2c1d31009f)
This commit is contained in:
Camilla Löwy 2023-12-12 17:38:52 +01:00 committed by Camilla Löwy
parent 628385e99e
commit 7b60753aeb
3 changed files with 11 additions and 3 deletions

View File

@ -83,6 +83,7 @@ video tutorials.
- Andrew Gutekanst - Andrew Gutekanst
- Stephen Gutekanst - Stephen Gutekanst
- Jonathan Hale - Jonathan Hale
- Daniel Hauser
- hdf89shfdfs - hdf89shfdfs
- Moritz Heinemann - Moritz Heinemann
- Sylvain Hellegouarch - Sylvain Hellegouarch

View File

@ -133,6 +133,8 @@ information on what to include when reporting a bug.
- [Cocoa] Bugfix: Full screen windows were resizable by the user (#2377,#2405) - [Cocoa] Bugfix: Full screen windows were resizable by the user (#2377,#2405)
- [Cocoa] Bugfix: Full screen windows were miniaturized when clicked on macOS - [Cocoa] Bugfix: Full screen windows were miniaturized when clicked on macOS
10.15 (#2377,#2405) 10.15 (#2377,#2405)
- [Cocoa] Bugfix: Querying joystick elements could reportedly segfault on macOS
13 Ventura (#2320)
- [Wayland] Added improved fallback window decorations via libdecor (#1639,#1693) - [Wayland] Added improved fallback window decorations via libdecor (#1639,#1693)
- [Wayland] Bugfix: Connecting a mouse after `glfwInit` would segfault (#1450) - [Wayland] Bugfix: Connecting a mouse after `glfwInit` would segfault (#1450)
- [Wayland] Disabled alpha channel for opaque windows on systems lacking - [Wayland] Disabled alpha channel for opaque windows on systems lacking

View File

@ -137,6 +137,14 @@ static void matchCallback(void* context,
return; return;
} }
CFArrayRef elements =
IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone);
// It is reportedly possible for this to fail on macOS 13 Ventura
// if the application does not have input monitoring permissions
if (!elements)
return;
axes = CFArrayCreateMutable(NULL, 0, NULL); axes = CFArrayCreateMutable(NULL, 0, NULL);
buttons = CFArrayCreateMutable(NULL, 0, NULL); buttons = CFArrayCreateMutable(NULL, 0, NULL);
hats = CFArrayCreateMutable(NULL, 0, NULL); hats = CFArrayCreateMutable(NULL, 0, NULL);
@ -180,9 +188,6 @@ static void matchCallback(void* context,
name[8], name[9], name[10]); name[8], name[9], name[10]);
} }
CFArrayRef elements =
IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone);
for (i = 0; i < CFArrayGetCount(elements); i++) for (i = 0; i < CFArrayGetCount(elements); i++)
{ {
IOHIDElementRef native = (IOHIDElementRef) IOHIDElementRef native = (IOHIDElementRef)