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];
strncpy(buffer, text, buffer_size);
[string release];
return 1;
}
}

View File

@ -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;
}