chore: Fix a bug and inline deserialize for safety

This commit is contained in:
Chris Hennick 2024-06-02 22:00:44 -07:00
parent eacc320fe0
commit 2725416c0d
No known key found for this signature in database
GPG key ID: DA47AABA4961C509
2 changed files with 17 additions and 18 deletions

View file

@ -102,23 +102,19 @@ pub(crate) trait FixedSizeBlock: Sized + Copy {
fn magic(self) -> Magic;
const ERROR: ZipError;
const WRONG_MAGIC_ERROR: ZipError;
/* TODO: use smallvec? */
fn interpret(bytes: Box<[u8]>) -> ZipResult<Self> {
let block = Self::deserialize(&bytes).from_le();
if block.magic() != Self::MAGIC {
return Err(Self::ERROR);
}
Ok(block)
}
fn deserialize(block: &[u8]) -> ZipResult<Self> {
if block.len() != mem::size_of::<Self>() {
if bytes.len() != mem::size_of::<Self>() {
return Err(ZipError::InvalidArchive("Block is wrong size"));
}
let block_ptr: *const Self = block.as_ptr().cast();
unsafe { block_ptr.read() }
let block_ptr: *const Self = bytes.as_ptr().cast();
let block = unsafe { block_ptr.read() }.from_le();
if block.magic() != Self::MAGIC {
return Err(Self::WRONG_MAGIC_ERROR);
}
Ok(block)
}
#[allow(clippy::wrong_self_convention)]
@ -222,7 +218,8 @@ impl FixedSizeBlock for Zip32CDEBlock {
self.magic
}
const ERROR: ZipError = ZipError::InvalidArchive("Invalid digital signature header");
const WRONG_MAGIC_ERROR: ZipError =
ZipError::InvalidArchive("Invalid digital signature header");
to_and_from_le![
(magic, Magic),
@ -401,7 +398,7 @@ impl FixedSizeBlock for Zip64CDELocatorBlock {
self.magic
}
const ERROR: ZipError =
const WRONG_MAGIC_ERROR: ZipError =
ZipError::InvalidArchive("Invalid zip64 locator digital signature header");
to_and_from_le![
@ -476,7 +473,8 @@ impl FixedSizeBlock for Zip64CDEBlock {
self.magic
}
const ERROR: ZipError = ZipError::InvalidArchive("Invalid digital signature header");
const WRONG_MAGIC_ERROR: ZipError =
ZipError::InvalidArchive("Invalid digital signature header");
to_and_from_le![
(magic, Magic),
@ -717,7 +715,7 @@ mod test {
self.magic
}
const ERROR: ZipError = ZipError::InvalidArchive("unreachable");
const WRONG_MAGIC_ERROR: ZipError = ZipError::InvalidArchive("unreachable");
to_and_from_le![(magic, Magic), (file_name_length, u16)];
}

View file

@ -899,7 +899,8 @@ impl FixedSizeBlock for ZipCentralEntryBlock {
self.magic
}
const ERROR: ZipError = ZipError::InvalidArchive("Invalid Central Directory header");
const WRONG_MAGIC_ERROR: ZipError =
ZipError::InvalidArchive("Invalid Central Directory header");
to_and_from_le![
(magic, spec::Magic),
@ -946,7 +947,7 @@ impl FixedSizeBlock for ZipLocalEntryBlock {
self.magic
}
const ERROR: ZipError = ZipError::InvalidArchive("Invalid local file header");
const WRONG_MAGIC_ERROR: ZipError = ZipError::InvalidArchive("Invalid local file header");
to_and_from_le![
(magic, spec::Magic),