Skip to content

Commit

Permalink
check if u32 -> usize respect usize boundaries
Browse files Browse the repository at this point in the history
which is necessary for u16 systems
  • Loading branch information
marcospb19 committed Jun 11, 2023
1 parent f24856c commit 56bcbee
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/matchers/archive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::convert::TryInto;
use std::convert::{TryFrom, TryInto};

/// Returns whether a buffer is an ePub.
pub fn is_epub(buf: &[u8]) -> bool {
Expand Down Expand Up @@ -217,12 +217,20 @@ pub fn is_zst(buf: &[u8]) -> bool {
return false;
}

let magic = u32::from_le_bytes(buf[0..4].try_into().unwrap()) as usize;
let magic = u32::from_le_bytes(buf[0..4].try_into().unwrap());
let Ok(magic) = usize::try_from(magic) else {
return false;
};

if magic & ZSTD_SKIP_MASK != ZSTD_SKIP_START {
return false;
}

let data_len = u32::from_le_bytes(buf[4..8].try_into().unwrap()) as usize;
let data_len = u32::from_le_bytes(buf[4..8].try_into().unwrap());
let Ok(data_len) = usize::try_from(data_len) else {
return false;
};

if buf.len() < 8 + data_len {
return false;
}
Expand Down

0 comments on commit 56bcbee

Please sign in to comment.