Skip to content

Commit

Permalink
fix: crash on invalid select-serial-port callback (#28618)
Browse files Browse the repository at this point in the history
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
  • Loading branch information
trop[bot] and codebytere committed Apr 12, 2021
1 parent 4efeaa0 commit e100c22
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 7 additions & 3 deletions shell/browser/serial/serial_chooser_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,13 @@ void SerialChooserController::OnDeviceChosen(const std::string& port_id) {
std::find_if(ports_.begin(), ports_.end(), [&port_id](const auto& ptr) {
return ptr->token.ToString() == port_id;
});
chooser_context_->GrantPortPermission(requesting_origin_, embedding_origin_,
*it->get());
RunCallback(it->Clone());
if (it != ports_.end()) {
chooser_context_->GrantPortPermission(requesting_origin_,
embedding_origin_, *it->get());
RunCallback(it->Clone());
} else {
RunCallback(/*port=*/nullptr);
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions spec-main/chromium-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,14 @@ describe('navigator.serial', () => {
expect(port).to.equal('NotFoundError: No port selected by the user.');
});

it('does not crash when select-serial-port is called with an invalid port', async () => {
w.webContents.session.on('select-serial-port', (event, portList, webContents, callback) => {
callback('i-do-not-exist');
});
const port = await getPorts();
expect(port).to.equal('NotFoundError: No port selected by the user.');
});

it('returns a port when select-serial-port event is defined', async () => {
w.webContents.session.on('select-serial-port', (event, portList, webContents, callback) => {
callback(portList[0].portId);
Expand Down

0 comments on commit e100c22

Please sign in to comment.