fix(misc): fix potential memory errors on macOS

This commit is contained in:
Federico Terzi 2022-02-13 17:24:52 +01:00
parent a70d9b6770
commit 81245722b8
2 changed files with 55 additions and 65 deletions

View File

@ -30,8 +30,6 @@ int32_t clipboard_get_text(char * buffer, int32_t buffer_size) {
const char * text = [string UTF8String]; const char * text = [string UTF8String];
strncpy(buffer, text, buffer_size); strncpy(buffer, text, buffer_size);
[string release];
return 1; return 1;
} }
} }

View File

@ -23,29 +23,27 @@
int32_t info_get_title(char *buffer, int32_t buffer_size) int32_t info_get_title(char *buffer, int32_t buffer_size)
{ {
@autoreleasepool { CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements | kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements | kCGWindowListOptionOnScreenOnly, kCGNullWindowID); int32_t result = 0;
int32_t result = 0;
if (windows) { if (windows) {
for (NSDictionary *window in (NSArray *)windows) { for (NSDictionary *window in (NSArray *)windows) {
NSNumber *ownerPid = window[(id) kCGWindowOwnerPID]; NSNumber *ownerPid = window[(id) kCGWindowOwnerPID];
NSRunningApplication *currentApp = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]]; NSRunningApplication *currentApp = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
if ([currentApp isActive]) { if ([currentApp isActive]) {
NSString *name = window[(id) kCGWindowName]; NSString *name = window[(id) kCGWindowName];
if (name.length > 0) { if (name.length > 0) {
const char * title = [name UTF8String]; const char * title = [name UTF8String];
snprintf(buffer, buffer_size, "%s", title); snprintf(buffer, buffer_size, "%s", title);
result = 1; result = 1;
}
break;
} }
break;
} }
CFRelease(windows);
} }
CFRelease(windows);
} }
return 0; 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 // 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) int32_t info_get_title_fallback(char *buffer, int32_t buffer_size)
{ {
@autoreleasepool { // Get the process ID of the frontmost application.
// Get the process ID of the frontmost application. NSRunningApplication* app = [[NSWorkspace sharedWorkspace] frontmostApplication];
NSRunningApplication* app = [[NSWorkspace sharedWorkspace] frontmostApplication]; pid_t pid = [app processIdentifier];
pid_t pid = [app processIdentifier];
AXUIElementRef appElem = AXUIElementCreateApplication(pid); AXUIElementRef appElem = AXUIElementCreateApplication(pid);
if (!appElem) { if (!appElem) {
return -1; return -1;
} }
// Get the accessibility element corresponding to the frontmost window // Get the accessibility element corresponding to the frontmost window
// of the frontmost application. // of the frontmost application.
AXUIElementRef window = NULL; AXUIElementRef window = NULL;
if (AXUIElementCopyAttributeValue(appElem, if (AXUIElementCopyAttributeValue(appElem,
kAXFocusedWindowAttribute, (CFTypeRef*)&window) != kAXErrorSuccess) { 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);
CFRelease(appElem); CFRelease(appElem);
return -2;
}
if (result != kAXErrorSuccess) { // Finally, get the title of the frontmost window.
// Failed to get the window title. CFStringRef title = NULL;
return -3; AXError result = AXUIElementCopyAttributeValue(window, kAXTitleAttribute,
} (CFTypeRef*)&title);
if (CFStringGetCString(title, buffer, buffer_size, kCFStringEncodingUTF8)) { // At this point, we don't need window and appElem anymore.
CFRelease(title); CFRelease(window);
return 1; CFRelease(appElem);
} else {
return -4; 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) int32_t info_get_exec(char *buffer, int32_t buffer_size)
{ {
@autoreleasepool { NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; NSString *bundlePath = [frontApp bundleURL].path;
NSString *bundlePath = [frontApp bundleURL].path; const char * path = [bundlePath UTF8String];
const char * path = [bundlePath UTF8String];
snprintf(buffer, buffer_size, "%s", path); snprintf(buffer, buffer_size, "%s", path);
}
return 1; return 1;
} }
int32_t info_get_class(char *buffer, int32_t buffer_size) int32_t info_get_class(char *buffer, int32_t buffer_size)
{ {
@autoreleasepool { NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; NSString *bundleId = frontApp.bundleIdentifier;
NSString *bundleId = frontApp.bundleIdentifier; const char * bundle = [bundleId UTF8String];
const char * bundle = [bundleId UTF8String];
snprintf(buffer, buffer_size, "%s", bundle); snprintf(buffer, buffer_size, "%s", bundle);
}
return 1; return 1;
} }