Skip to content

Commit 411ae27

Browse files
committed
Reject malformed blocks for ByteStream rather than panic.
1 parent 653e3b3 commit 411ae27

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/lib.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
//!
6868
#![no_std]
6969
#![feature(type_alias_impl_trait)]
70+
#![feature(cell_filter_map)]
7071
#![feature(cfg_version)]
7172
#![cfg_attr(
7273
all(target_family = "bolos", not(version("1.64"))),
@@ -348,9 +349,13 @@ impl Block {
348349
unsafe { &*block }
349350
}
350351

351-
pub fn from_raw_slice(block: &[u8]) -> &Self {
352-
let block2 = block as *const [u8] as *const Block;
353-
unsafe { &*block2 }
352+
pub fn from_raw_slice_opt(block: &[u8]) -> Option<&Self> {
353+
if block.len() >= HASH_LEN {
354+
let block2 = block as *const [u8] as *const Block;
355+
Some(unsafe { &*block2 })
356+
} else {
357+
None
358+
}
354359
}
355360

356361
/// Panics if block is illegally short
@@ -382,10 +387,12 @@ impl ByteStream {
382387
}
383388
let chunk_res = self.host_io.get_chunk(self.current_chunk).await;
384389
match chunk_res {
385-
Ok(a) => Ref::map(a, Block::from_raw_slice),
390+
Ok(a) => match Ref::filter_map(a, Block::from_raw_slice_opt) {
391+
Ok(r) => r,
392+
Err(_) => reject().await,
393+
}
386394
Err(_) => reject().await,
387395
}
388-
//return Ref::map(chunk, |r| &r[self.current_offset + HASH_LEN..]);
389396
}
390397
}
391398

0 commit comments

Comments
 (0)