Skip to content

Commit fb6f604

Browse files
authored
fix: heap-use-after-free in tray.popUpContextMenu (electron#22842) (electron#23182)
1 parent ca11175 commit fb6f604

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

shell/browser/ui/tray_icon_cocoa.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,16 @@ - (void)popUpContextMenu:(electron::AtomMenuModel*)menu_model {
173173
useDefaultAccelerator:NO]);
174174
// Hacky way to mimic design of ordinary tray menu.
175175
[statusItem_ setMenu:[menuController menu]];
176+
// -performClick: is a blocking call, which will run the task loop inside
177+
// itself. This can potentially include running JS, which can result in
178+
// this object being released. We take a temporary reference here to make
179+
// sure we stay alive long enough to successfully return from this
180+
// function.
181+
// TODO(nornagon/codebytere): Avoid nesting task loops here.
182+
[self retain];
176183
[[statusItem_ button] performClick:self];
177184
[statusItem_ setMenu:[menuController_ menu]];
185+
[self release];
178186
return;
179187
}
180188

spec-main/api-tray-spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ describe('tray module', () => {
5151
})
5252
tray.popUpContextMenu()
5353
})
54+
55+
it('can be called with a menu', () => {
56+
const menu = Menu.buildFromTemplate([{ label: 'Test' }])
57+
expect(() => {
58+
tray.popUpContextMenu(menu)
59+
}).to.not.throw()
60+
})
5461
})
5562

5663
describe('tray.getBounds()', () => {

0 commit comments

Comments
 (0)