Skip to content

Commit

Permalink
dont panic on invalid DiskAddress length (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Laine authored Mar 20, 2024
1 parent 16410d3 commit a6f817b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
9 changes: 7 additions & 2 deletions firewood/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,19 @@ impl DbHeader {

impl Storable for DbHeader {
fn deserialize<T: CachedStore>(addr: usize, mem: &T) -> Result<Self, shale::ShaleError> {
let raw = mem
let root_bytes = mem
.get_view(addr, Self::MSIZE)
.ok_or(ShaleError::InvalidCacheView {
offset: addr,
size: Self::MSIZE,
})?;
let root_bytes = root_bytes.as_deref();
let root_bytes = root_bytes.as_slice();

Ok(Self {
kv_root: raw.as_deref().as_slice().into(),
kv_root: root_bytes
.try_into()
.expect("Self::MSIZE == DiskAddress:MSIZE"),
})
}

Expand Down
19 changes: 13 additions & 6 deletions firewood/src/shale/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,21 @@ impl Storable for CompactSpaceHeader {
size: Self::MSIZE,
})?;
#[allow(clippy::indexing_slicing)]
let meta_space_tail = raw.as_deref()[..Self::DATA_SPACE_TAIL_OFFSET].into();
let meta_space_tail = raw.as_deref()[..Self::DATA_SPACE_TAIL_OFFSET]
.try_into()
.expect("Self::MSIZE = 4 * DiskAddress::MSIZE");
#[allow(clippy::indexing_slicing)]
let data_space_tail =
raw.as_deref()[Self::DATA_SPACE_TAIL_OFFSET..Self::BASE_ADDR_OFFSET].into();
let data_space_tail = raw.as_deref()[Self::DATA_SPACE_TAIL_OFFSET..Self::BASE_ADDR_OFFSET]
.try_into()
.expect("Self::MSIZE = 4 * DiskAddress::MSIZE");
#[allow(clippy::indexing_slicing)]
let base_addr = raw.as_deref()[Self::BASE_ADDR_OFFSET..Self::ALLOC_ADDR_OFFSET].into();
let base_addr = raw.as_deref()[Self::BASE_ADDR_OFFSET..Self::ALLOC_ADDR_OFFSET]
.try_into()
.expect("Self::MSIZE = 4 * DiskAddress::MSIZE");
#[allow(clippy::indexing_slicing)]
let alloc_addr = raw.as_deref()[Self::ALLOC_ADDR_OFFSET..].into();
let alloc_addr = raw.as_deref()[Self::ALLOC_ADDR_OFFSET..]
.try_into()
.expect("Self::MSIZE = 4 * DiskAddress::MSIZE");
Ok(Self {
meta_space_tail,
data_space_tail,
Expand Down Expand Up @@ -656,7 +663,7 @@ impl<T: Storable + Debug + 'static, M: CachedStore> CompactSpace<T, M> {
#[allow(clippy::unwrap_used)]
if ptr < DiskAddress::from(CompactSpaceHeader::MSIZE as usize) {
return Err(ShaleError::InvalidAddressLength {
expected: DiskAddress::from(CompactSpaceHeader::MSIZE as usize),
expected: CompactSpaceHeader::MSIZE,
found: ptr.0.map(|inner| inner.get()).unwrap_or_default() as u64,
});
}
Expand Down
11 changes: 6 additions & 5 deletions firewood/src/shale/disk_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ impl From<[u8; 8]> for DiskAddress {
/// Convert from a slice of bytes to a DiskAddress
/// panics if the slice isn't 8 bytes; used for
/// serialization from disk
impl From<&[u8]> for DiskAddress {
fn from(value: &[u8]) -> Self {
#[allow(clippy::unwrap_used)]
let bytes: [u8; Self::MSIZE as usize] = value.try_into().unwrap();
bytes.into()
impl TryFrom<&[u8]> for DiskAddress {
type Error = std::array::TryFromSliceError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let bytes: [u8; Self::MSIZE as usize] = value.try_into()?;
Ok(bytes.into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion firewood/src/shale/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum ShaleError {
error: &'static str,
},
#[error("invalid address length expected: {expected:?} found: {found:?})")]
InvalidAddressLength { expected: DiskAddress, found: u64 },
InvalidAddressLength { expected: u64, found: u64 },
#[error("invalid node type")]
InvalidNodeType,
#[error("invalid node metadata")]
Expand Down

0 comments on commit a6f817b

Please sign in to comment.