Skip to content

Commit

Permalink
refactor: Verify with debug assertions that no FixedSizeBlock expects…
Browse files Browse the repository at this point in the history
… a multi-byte alignment (#198)
  • Loading branch information
Pr0methean committed Jun 22, 2024
1 parent 26e6462 commit 8bb3be0
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/spec.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#![macro_use]

use crate::result::{ZipError, ZipResult};
use core::mem;
use core::mem::align_of;
use memchr::memmem::FinderRev;
use std::io;
use std::io::prelude::*;
use std::mem;
use std::rc::Rc;

/// "Magic" header values used in the zip spec to locate metadata records.
Expand Down Expand Up @@ -109,6 +110,10 @@ pub(crate) trait FixedSizeBlock: Sized + Copy {
return Err(ZipError::InvalidArchive("Block is wrong size"));
}
let block_ptr: *const Self = bytes.as_ptr().cast();

// If alignment could be more than 1, we'd have to use read_unaligned() below
debug_assert_eq!(align_of::<Self>(), 1);

let block = unsafe { block_ptr.read() }.from_le();
if block.magic() != Self::MAGIC {
return Err(Self::WRONG_MAGIC_ERROR);
Expand All @@ -135,6 +140,10 @@ pub(crate) trait FixedSizeBlock: Sized + Copy {
fn serialize(self) -> Box<[u8]> {
/* TODO: use Box::new_zeroed() when stabilized! */
/* TODO: also consider using smallvec! */

// If alignment could be more than 1, we'd have to use write_unaligned() below
debug_assert_eq!(align_of::<Self>(), 1);

let mut out_block = vec![0u8; mem::size_of::<Self>()].into_boxed_slice();
let out_ptr: *mut Self = out_block.as_mut_ptr().cast();
unsafe {
Expand Down

0 comments on commit 8bb3be0

Please sign in to comment.