From 791be4766a07d0da7ee971ed57101114a77d3f04 Mon Sep 17 00:00:00 2001 From: LD Date: Thu, 28 Nov 2019 23:37:33 +0100 Subject: [PATCH] MacOS: Don't crash if no elements are returned by IOHIDDeviceCopyMatchingElements Fixes #797 --- panda/src/device/ioKitInputDevice.cxx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/panda/src/device/ioKitInputDevice.cxx b/panda/src/device/ioKitInputDevice.cxx index 8468fd4816..1d8bf38f0f 100644 --- a/panda/src/device/ioKitInputDevice.cxx +++ b/panda/src/device/ioKitInputDevice.cxx @@ -114,12 +114,14 @@ IOKitInputDevice(IOHIDDeviceRef device) : } CFArrayRef elements = IOHIDDeviceCopyMatchingElements(device, nullptr, 0); - CFIndex count = CFArrayGetCount(elements); - for (CFIndex i = 0; i < count; ++i) { - IOHIDElementRef element = (IOHIDElementRef)CFArrayGetValueAtIndex(elements, i); - parse_element(element); + if (elements) { + CFIndex count = CFArrayGetCount(elements); + for (CFIndex i = 0; i < count; ++i) { + IOHIDElementRef element = (IOHIDElementRef)CFArrayGetValueAtIndex(elements, i); + parse_element(element); + } + CFRelease(elements); } - CFRelease(elements); if (_hat_element != nullptr) { _hat_left_button = (int)_buttons.size();