macOS: Work around [NSApplication setWindowsMenu:] out of bound access

The implementation of [NSApplication setWindowsMenu:] seems to look
for the last item in the menu, but doesn't guard the check for the
menu having items. Instead it guards on another array being non-empty,
and in some situation this array has items of type NSWindowMenuItem
while our window menu is empty (FB13369198).

To work around this we insert a hidden dummy item into the menu.

Fixes: PYSIDE-2525
Pick-to: 6.5 6.6
Change-Id: Iaa9dbc9454249f4eb34f8a338d0cc23685f0025a
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Tor Arne Vestbø 2023-11-13 20:02:11 +01:00
parent 189f9873ae
commit 0a0f7b864b

View File

@ -362,6 +362,15 @@ void QCocoaMenuBar::insertWindowMenu()
winMenuItem.hidden = YES;
winMenuItem.submenu = [[[NSMenu alloc] initWithTitle:@"QtWindowMenu"] autorelease];
// AppKit has a bug in [NSApplication setWindowsMenu:] where it will resolve
// the last item of the window menu's itemArray, but not account for the array
// being empty, resulting in a lookup of itemAtIndex:-1. To work around this,
// we insert a hidden dummy item into the menu. See FB13369198.
auto *dummyItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
dummyItem.hidden = YES;
[winMenuItem.submenu addItem:[dummyItem autorelease]];
[mainMenu insertItem:winMenuItem atIndex:mainMenu.itemArray.count];
app.windowsMenu = winMenuItem.submenu;