Merge pull request #751 from PekingSpades/bugfix-macos-keyboard-crash

Fix: fix SIGSEGV/SIGBUS crash in macOS keyboard functions
This commit is contained in:
Evans 2025-12-28 09:05:03 -08:00 committed by GitHub
commit 5a3888436b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -38,8 +38,12 @@ MMKeyCode keyCodeForChar(const char c) {
charStr = CFStringCreateWithCharacters(kCFAllocatorDefault, &character, 1);
/* Our values may be NULL (0), so we need to use this function. */
if (!CFDictionaryGetValueIfPresent(charToCodeDict, charStr, (const void **)&code)) {
/* Use pointer-sized variable to avoid stack overflow on 64-bit systems */
const void *codePtr = NULL;
if (!CFDictionaryGetValueIfPresent(charToCodeDict, charStr, &codePtr)) {
code = UINT16_MAX; /* Error */
} else {
code = (CGKeyCode)(uintptr_t)codePtr;
}
CFRelease(charStr);
@ -92,10 +96,17 @@ MMKeyCode keyCodeForChar(const char c) {
CFStringRef createStringForKey(CGKeyCode keyCode){
// TISInputSourceRef currentKeyboard = TISCopyCurrentASCIICapableKeyboardInputSource();
TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardLayoutInputSource();
/* Check if currentKeyboard is NULL to avoid crash */
if (currentKeyboard == NULL) { return NULL; }
CFDataRef layoutData = (CFDataRef) TISGetInputSourceProperty(
currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
if (layoutData == nil) { return 0; }
if (layoutData == nil) {
CFRelease(currentKeyboard); /* Fix memory leak */
return NULL;
}
const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout *) CFDataGetBytePtr(layoutData);
UInt32 keysDown = 0;