fix: CHECK when adding view as its own child (#42107)

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot] 2024-05-09 12:33:21 -04:00 committed by GitHub
parent 73046231c0
commit 0650ffc715
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 1 deletions

View file

@ -189,6 +189,14 @@ void View::AddChildViewAt(gin::Handle<View> child,
// has a View, possibly a wrapper view around the underlying platform View. // has a View, possibly a wrapper view around the underlying platform View.
if (!view_) if (!view_)
return; return;
// This will CHECK and crash in View::AddChildViewAtImpl if not handled here.
if (view_ == child->view()) {
gin_helper::ErrorThrower(isolate()).ThrowError(
"A view cannot be added as its own child");
return;
}
size_t index = size_t index =
std::min(child_views_.size(), maybe_index.value_or(child_views_.size())); std::min(child_views_.size(), maybe_index.value_or(child_views_.size()));
child_views_.emplace(child_views_.begin() + index, // index child_views_.emplace(child_views_.begin() + index, // index

View file

@ -1,3 +1,4 @@
import { expect } from 'chai';
import { closeWindow } from './lib/window-helpers'; import { closeWindow } from './lib/window-helpers';
import { BaseWindow, View } from 'electron/main'; import { BaseWindow, View } from 'electron/main';
@ -10,6 +11,15 @@ describe('View', () => {
it('can be used as content view', () => { it('can be used as content view', () => {
w = new BaseWindow({ show: false }); w = new BaseWindow({ show: false });
w.setContentView(new View()); const v = new View();
w.setContentView(v);
expect(w.contentView).to.equal(v);
});
it('will throw when added as a child to itself', () => {
w = new BaseWindow({ show: false });
expect(() => {
w.contentView.addChildView(w.contentView);
}).to.throw('A view cannot be added as its own child');
}); });
}); });