From 2c1d31009fdc527d5a44ff887659a25ecbf1f7f0 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 --- 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 0b66f2ef..75533ec3 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -85,6 +85,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 831bdba6..cebe62bc 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,8 @@ information on what to include when reporting a bug. subdirectory (#2113,#2120) - [Cocoa] Bugfix: Compilation failed on OS X 10.8 due to unconditional use of 10.9+ symbols (#2161) + - [Cocoa] Bugfix: Querying joystick elements could reportedly segfault on macOS + 13 Ventura (#2320) - [X11] Bugfix: The CMake files did not check for the XInput headers (#1480) - [X11] Bugfix: Key names were not updated when the keyboard layout changed (#1462,#1528) diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m index 865adac7..caeb4725 100644 --- a/src/cocoa_joystick.m +++ b/src/cocoa_joystick.m @@ -136,6 +136,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); @@ -179,9 +187,6 @@ static void matchCallback(void* context, name[8], name[9], name[10]); } - CFArrayRef elements = - IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone); - for (CFIndex i = 0; i < CFArrayGetCount(elements); i++) { IOHIDElementRef native = (IOHIDElementRef)