Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential missed guarantee for byte-order in byte slice casting #640

Open
shinmao opened this issue Jun 6, 2023 · 0 comments
Open

Potential missed guarantee for byte-order in byte slice casting #640

shinmao opened this issue Jun 6, 2023 · 0 comments

Comments

@shinmao
Copy link

shinmao commented Jun 6, 2023

impl<'s> From<&'s mut BufData> for &'s mut DImapBlock {
fn from(b: &'s mut BufData) -> Self {
const_assert!(mem::size_of::<DImapBlock>() <= mem::size_of::<BufData>());
const_assert!(mem::align_of::<BufData>() % mem::align_of::<DImapBlock>() == 0);
unsafe { &mut *(b.as_mut_ptr() as *mut DImapBlock) }
}
}

In From implementation above, we can cast from [u8, BSIZE] to [u32; NENTRY]. In addition to checking size and alignment, the results of casting around bytes between data types are endian dependent, and I couldn't find any specification about byte-order here. What's more, the u32 byte slice is mutable, which means we can change the u32 byte slice value and affect the u8 byte slice by different order again.

Maybe using byteorder could help solving this kind of issues.

Please correct me if I mistake for something;)

reference: https://docs.rs/bytemuck/latest/bytemuck/trait.Pod.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant