Fix: fix stack overflow and memory leak in macOS keycode functions

This commit is contained in:
PekingSpades 2025-12-22 16:33:02 +08:00
parent 381e4bd37f
commit 2488530e08

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;