Crash - NSScreen.screens array accessed without bounds check
Crash - NSScreen.screens array accessed without bounds check
Labels: bug, crash, low-priority
Description
NotificationManager accesses NSScreen.screens[0] as a fallback without checking if the array is empty, which crashes in headless mode or when all screens are disconnected.
User Impact
-
Crash if app runs in headless mode or when all screens disconnected
-
Edge case but can occur in server environments or during screen configuration changes
-
Affects users with unusual multi-monitor setups during transitions
Technical Details
File: VoiceInk/Notifications/NotificationManager.swift
Line: 83
let activeScreen = NSApp.keyWindow?.screen ?? NSScreen.main ?? NSScreen.screens[0]
The method is correctly marked @MainActor (line 81), so thread safety is not an issue. The problem is the array subscript on NSScreen.screens[0] which will crash if no screens are available.
Reproduction
-
Run app in headless mode (no display connected)
-
Trigger a notification
-
Observe crash on array access
Recommended Fix
Replace line 83 in VoiceInk/Notifications/NotificationManager.swift:
let activeScreen: NSScreen
if let windowScreen = NSApp.keyWindow?.screen {
activeScreen = windowScreen
} else if let mainScreen = NSScreen.main {
activeScreen = mainScreen
} else if let firstScreen = NSScreen.screens.first {
activeScreen = firstScreen
} else {
// No screens available - cannot position notification
return
}
Testing
-
Test with multiple monitors
-
Test connecting/disconnecting displays during app usage
-
Verify notifications appear correctly in all scenarios
-
Test in VM or headless environment if possible