From 49d228207f8322ed8aa9f820a1490c852a5cb2d7 Mon Sep 17 00:00:00 2001 From: Aaron Jacobs Date: Mon, 8 Feb 2016 11:21:48 -0800 Subject: [PATCH] Fix crash when joystick name unavailable on OS X Fixes #694. Closes #701. --- README.md | 1 + src/cocoa_joystick.m | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b9eb5576..0d2e30f4 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ used by the tests and examples and are not required to build the library. when no windows existed - [Cocoa] Removed support for OS X 10.6 - [Cocoa] Bugfix: Full screen windows on secondary monitors were mispositioned + - [Cocoa] Bugfix: Connecting a joystick that reports no name would segfault - [X11] Bugfix: Monitor connection and disconnection events were not reported - [X11] Bugfix: Decoding of UTF-8 text from XIM could continue past the end - [X11] Bugfix: An XKB structure was leaked during `glfwInit` diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m index 0229ccec..5c584f6d 100644 --- a/src/cocoa_joystick.m +++ b/src/cocoa_joystick.m @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -287,10 +288,15 @@ static void matchCallback(void* context, CFStringRef name = IOHIDDeviceGetProperty(deviceRef, CFSTR(kIOHIDProductKey)); - CFStringGetCString(name, - joystick->name, - sizeof(joystick->name), - kCFStringEncodingUTF8); + if (name) + { + CFStringGetCString(name, + joystick->name, + sizeof(joystick->name), + kCFStringEncodingUTF8); + } + else + strncpy(joystick->name, "Unknown", sizeof(joystick->name)); joystick->axisElements = CFArrayCreateMutable(NULL, 0, NULL); joystick->buttonElements = CFArrayCreateMutable(NULL, 0, NULL);