From 7b60753aeb839a7fe0c3f834e4549a49d0bb8f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Tue, 12 Dec 2023 17:38:52 +0100 Subject: [PATCH] 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 2c1d31009fdc527d5a44ff887659a25ecbf1f7f0) --- CONTRIBUTORS.md | 1 + README.md | 2 ++ src/cocoa_joystick.m | 11 ++++++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 0a8ca8e0..01783375 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -83,6 +83,7 @@ video tutorials. - Andrew Gutekanst - Stephen Gutekanst - Jonathan Hale + - Daniel Hauser - hdf89shfdfs - Moritz Heinemann - Sylvain Hellegouarch diff --git a/README.md b/README.md index 91e9166c..a527d70b 100644 --- a/README.md +++ b/README.md @@ -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 miniaturized when clicked on macOS 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] Bugfix: Connecting a mouse after `glfwInit` would segfault (#1450) - [Wayland] Disabled alpha channel for opaque windows on systems lacking diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m index 3d306777..f91cf2f8 100644 --- a/src/cocoa_joystick.m +++ b/src/cocoa_joystick.m @@ -137,6 +137,14 @@ static void matchCallback(void* context, 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); buttons = CFArrayCreateMutable(NULL, 0, NULL); hats = CFArrayCreateMutable(NULL, 0, NULL); @@ -180,9 +188,6 @@ static void matchCallback(void* context, name[8], name[9], name[10]); } - CFArrayRef elements = - IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone); - for (i = 0; i < CFArrayGetCount(elements); i++) { IOHIDElementRef native = (IOHIDElementRef)