qt: Avoid implicit `NSApplication` instantiation
What changed, and why it matters
This commit fixes a macOS-specific behavior in Bitcoin Core's Qt interface. Previously, two pieces of code called [NSApplication sharedApplication], which has the side effect of creating the macOS application object if it didn't already exist. When running Bitcoin Core's GUI in headless/minimal test modes on macOS, this unintended creation could cause subtle state problems. The fix uses the NSApp global instead and skips the calls if no application object exists. It is a defensive bug fix rather than a clear-cut security vulnerability.
Treat as a low-risk hardening fix. No immediate security response is warranted, but include it in routine release notes as a macOS/Qt robustness improvement. If running automated GUI tests on macOS with minimal/offscreen plugins, verify behavior after the patch.
Security signals we found
Implicit object instantiation side effect removed
Defensive nil check added before Objective-C message send
macOS-only Qt platform code changed
Affects non-standard QPA plugin environments (minimal/offscreen)
Evidence from the diff
In src/qt/macdockiconhandler.mm, setupDockClickHandler() and ForceActivation() previously invoked [NSApplication sharedApplication]. That method lazily instantiates NSApplication. Under the minimal or offscreen QPA plugins, the Cocoa platform plugin does not create NSApplication, so these call sites were creating it implicitly. The patch replaces those calls with the NSApp global and adds nil checks, returning early when NSApplication is absent. This prevents unintended NSApplication instantiation during automated or non-standard GUI runs on macOS.
Changed components
src/qt/macdockiconhandler.mmmacOS dock click handler setupmacOS window force-activation helperInspect captured patch +4 / −2
diff --git a/src/qt/macdockiconhandler.mm b/src/qt/macdockiconhandler.mm
index b7b1b98e..1b62032c 100644
--- a/src/qt/macdockiconhandler.mm
+++ b/src/qt/macdockiconhandler.mm
@@ -20,7 +20,8 @@ bool dockClickHandler(id self, SEL _cmd, ...) {
}
void setupDockClickHandler() {
- Class delClass = (Class)[[[NSApplication sharedApplication] delegate] class];
+ if (NSApp == nil) return;
+ Class delClass = (Class)[[NSApp delegate] class];
SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:");
}
@@ -49,5 +50,6 @@ void MacDockIconHandler::cleanup()
*/
void ForceActivation()
{
- [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
+ if (NSApp == nil) return;
+ [NSApp activateIgnoringOtherApps:YES];
}
Why this scored 26/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.