Skip to content

Commit a2f22e5

Browse files
committed
Fix unwraps
1 parent 9fb7b2e commit a2f22e5

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

crates/ark/src/console/console_comm.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ impl Console {
5151
}
5252

5353
pub(super) fn comm_handle_close(&self, comm_id: &str) {
54-
if self.is_ui_comm(comm_id) {
55-
let ui = self.take_ui_comm().unwrap();
54+
if let Some(ui) = self.take_ui_comm_if(comm_id) {
5655
ui.handler.into_inner().handle_close(&ui.ctx);
5756
return;
5857
}
@@ -189,6 +188,17 @@ impl Console {
189188
self.ui_comm.borrow_mut().take()
190189
}
191190

191+
/// Take the UI comm only if its `comm_id` matches. Checks and takes
192+
/// in a single `borrow_mut()` so there is no TOCTOU gap.
193+
fn take_ui_comm_if(&self, comm_id: &str) -> Option<ConsoleComm> {
194+
let mut guard = self.ui_comm.borrow_mut();
195+
if guard.as_ref().is_some_and(|ui| ui.comm_id == comm_id) {
196+
guard.take()
197+
} else {
198+
None
199+
}
200+
}
201+
192202
fn set_ui_comm(&self, ui: ConsoleComm) {
193203
*self.ui_comm.borrow_mut() = Some(ui);
194204
}

crates/ark/src/plots/graphics_device.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,9 @@ impl DeviceContext {
704704
display_id: id.to_string(),
705705
data: None,
706706
};
707-
let transient = serde_json::to_value(transient).unwrap();
707+
let Some(transient) = serde_json::to_value(transient).log_err() else {
708+
return;
709+
};
708710

709711
log::info!("Sending display data to IOPub.");
710712

@@ -778,7 +780,10 @@ impl DeviceContext {
778780
},
779781
};
780782

781-
let value = serde_json::to_value(PlotFrontendEvent::Update(update_params)).unwrap();
783+
let Some(value) = serde_json::to_value(PlotFrontendEvent::Update(update_params)).log_err()
784+
else {
785+
return;
786+
};
782787

783788
let outgoing_tx = CommOutgoingTx::new(comm_id, self.iopub_tx.clone());
784789
outgoing_tx
@@ -841,7 +846,7 @@ impl DeviceContext {
841846
});
842847

843848
let mut map = serde_json::Map::new();
844-
map.insert("image/png".to_string(), serde_json::to_value(data).unwrap());
849+
map.insert("image/png".to_string(), serde_json::to_value(data)?);
845850

846851
Ok(serde_json::Value::Object(map))
847852
}

0 commit comments

Comments
 (0)