From 81245722b8d6a275b3844a5d81d647a27d9eceb8 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 13 Feb 2022 17:24:52 +0100 Subject: [PATCH] fix(misc): fix potential memory errors on macOS --- espanso-clipboard/src/cocoa/native.mm | 2 - espanso-info/src/cocoa/native.mm | 118 ++++++++++++-------------- 2 files changed, 55 insertions(+), 65 deletions(-) diff --git a/espanso-clipboard/src/cocoa/native.mm b/espanso-clipboard/src/cocoa/native.mm index 91ed16a..a5bbf38 100644 --- a/espanso-clipboard/src/cocoa/native.mm +++ b/espanso-clipboard/src/cocoa/native.mm @@ -30,8 +30,6 @@ int32_t clipboard_get_text(char * buffer, int32_t buffer_size) { const char * text = [string UTF8String]; strncpy(buffer, text, buffer_size); - [string release]; - return 1; } } diff --git a/espanso-info/src/cocoa/native.mm b/espanso-info/src/cocoa/native.mm index 86d9403..b437952 100644 --- a/espanso-info/src/cocoa/native.mm +++ b/espanso-info/src/cocoa/native.mm @@ -23,29 +23,27 @@ int32_t info_get_title(char *buffer, int32_t buffer_size) { - @autoreleasepool { - CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements | kCGWindowListOptionOnScreenOnly, kCGNullWindowID); - int32_t result = 0; + CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements | kCGWindowListOptionOnScreenOnly, kCGNullWindowID); + int32_t result = 0; - if (windows) { - for (NSDictionary *window in (NSArray *)windows) { - NSNumber *ownerPid = window[(id) kCGWindowOwnerPID]; + if (windows) { + for (NSDictionary *window in (NSArray *)windows) { + NSNumber *ownerPid = window[(id) kCGWindowOwnerPID]; - NSRunningApplication *currentApp = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]]; + NSRunningApplication *currentApp = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]]; - if ([currentApp isActive]) { - NSString *name = window[(id) kCGWindowName]; - if (name.length > 0) { - const char * title = [name UTF8String]; - snprintf(buffer, buffer_size, "%s", title); - result = 1; - } - break; + if ([currentApp isActive]) { + NSString *name = window[(id) kCGWindowName]; + if (name.length > 0) { + const char * title = [name UTF8String]; + snprintf(buffer, buffer_size, "%s", title); + result = 1; } + break; } - - CFRelease(windows); } + + CFRelease(windows); } return 0; @@ -54,70 +52,64 @@ int32_t info_get_title(char *buffer, int32_t buffer_size) // Partially taken from: https://stackoverflow.com/questions/480866/get-the-title-of-the-current-active-window-document-in-mac-os-x/23451568#23451568 int32_t info_get_title_fallback(char *buffer, int32_t buffer_size) { - @autoreleasepool { - // Get the process ID of the frontmost application. - NSRunningApplication* app = [[NSWorkspace sharedWorkspace] frontmostApplication]; - pid_t pid = [app processIdentifier]; + // Get the process ID of the frontmost application. + NSRunningApplication* app = [[NSWorkspace sharedWorkspace] frontmostApplication]; + pid_t pid = [app processIdentifier]; - AXUIElementRef appElem = AXUIElementCreateApplication(pid); - if (!appElem) { - return -1; - } + AXUIElementRef appElem = AXUIElementCreateApplication(pid); + if (!appElem) { + return -1; + } - // Get the accessibility element corresponding to the frontmost window - // of the frontmost application. - AXUIElementRef window = NULL; - if (AXUIElementCopyAttributeValue(appElem, - kAXFocusedWindowAttribute, (CFTypeRef*)&window) != kAXErrorSuccess) { - CFRelease(appElem); - return -2; - } - - // Finally, get the title of the frontmost window. - CFStringRef title = NULL; - AXError result = AXUIElementCopyAttributeValue(window, kAXTitleAttribute, - (CFTypeRef*)&title); - - // At this point, we don't need window and appElem anymore. - CFRelease(window); + // Get the accessibility element corresponding to the frontmost window + // of the frontmost application. + AXUIElementRef window = NULL; + if (AXUIElementCopyAttributeValue(appElem, + kAXFocusedWindowAttribute, (CFTypeRef*)&window) != kAXErrorSuccess) { CFRelease(appElem); + return -2; + } - if (result != kAXErrorSuccess) { - // Failed to get the window title. - return -3; - } + // Finally, get the title of the frontmost window. + CFStringRef title = NULL; + AXError result = AXUIElementCopyAttributeValue(window, kAXTitleAttribute, + (CFTypeRef*)&title); - if (CFStringGetCString(title, buffer, buffer_size, kCFStringEncodingUTF8)) { - CFRelease(title); - return 1; - } else { - return -4; - } + // At this point, we don't need window and appElem anymore. + CFRelease(window); + CFRelease(appElem); + + if (result != kAXErrorSuccess) { + // Failed to get the window title. + return -3; + } + + if (CFStringGetCString(title, buffer, buffer_size, kCFStringEncodingUTF8)) { + CFRelease(title); + return 1; + } else { + return -4; } } int32_t info_get_exec(char *buffer, int32_t buffer_size) { - @autoreleasepool { - NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; - NSString *bundlePath = [frontApp bundleURL].path; - const char * path = [bundlePath UTF8String]; + NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; + NSString *bundlePath = [frontApp bundleURL].path; + const char * path = [bundlePath UTF8String]; - snprintf(buffer, buffer_size, "%s", path); - } + snprintf(buffer, buffer_size, "%s", path); return 1; } int32_t info_get_class(char *buffer, int32_t buffer_size) { - @autoreleasepool { - NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; - NSString *bundleId = frontApp.bundleIdentifier; - const char * bundle = [bundleId UTF8String]; + NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; + NSString *bundleId = frontApp.bundleIdentifier; + const char * bundle = [bundleId UTF8String]; - snprintf(buffer, buffer_size, "%s", bundle); - } + snprintf(buffer, buffer_size, "%s", bundle); return 1; } \ No newline at end of file