Skip to content

Commit b10727a

Browse files
committed
Fail serialization if shmem region index is out of bounds
Signed-off-by: Simon Wülker <[email protected]>
1 parent 82f6c49 commit b10727a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/ipc.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::platform::{
1313
};
1414

1515
use bincode;
16-
use serde::{Deserialize, Deserializer, Serialize, Serializer};
16+
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
1717
use std::cell::RefCell;
1818
use std::cmp::min;
1919
use std::error::Error as StdError;
@@ -579,13 +579,15 @@ impl<'de> Deserialize<'de> for IpcSharedMemory {
579579

580580
let os_shared_memory = OS_IPC_SHARED_MEMORY_REGIONS_FOR_DESERIALIZATION.with(
581581
|os_ipc_shared_memory_regions_for_deserialization| {
582-
// FIXME(pcwalton): This could panic if the data was corrupt and the index was out
583-
// of bounds. We should return an `Err` result instead.
584-
os_ipc_shared_memory_regions_for_deserialization.borrow_mut()[index]
585-
.take()
586-
.unwrap()
582+
let mut regions = os_ipc_shared_memory_regions_for_deserialization.borrow_mut();
583+
let Some(region) = regions.get_mut(index) else {
584+
return Err(format!("Cannot consume shared memory region {index}, there are only {} regions available", regions.len()));
585+
};
586+
587+
region.take().ok_or_else(|| format!("Shared memory region {index} has already been consumed"))
587588
},
588-
);
589+
).map_err(D::Error::custom)?;
590+
589591
Ok(IpcSharedMemory {
590592
os_shared_memory: Some(os_shared_memory),
591593
})

0 commit comments

Comments
 (0)