Skip to content

Unsoundness in read_packed_fixed: Unaligned memory access in v0.8.1 #269

Description

@shinmao

Hi, I import the crate of quick-protobuf v0.8.1 and accidentally trigger the error with read_packed_fixed::<u32>. However, I found that the code shown in crates.io is different from the one on Github. It seems that the code on crates.io is newer than here?

pub fn read_packed_fixed<'a, M>(&mut self, bytes: &'a [u8]) -> Result<&'a [M]> {
        let len = self.read_varint32(bytes)? as usize;
        if self.len() < len {
            return Err(Error::UnexpectedEndOfBuffer);
        }
        let n = len / ::core::mem::size_of::<M>();
        let slice = unsafe {
            ::core::slice::from_raw_parts(
                bytes.get_unchecked(self.start) as *const u8 as *const M,
                n,
            )
        };
        self.start += len;
        Ok(slice)
}

The code allow to set arbitrary type to generic type without trait bound, which could cause to undefined behavior here.
See crate code here: https://docs.rs/crate/quick-protobuf/0.8.1/source/src/reader.rs#443-462

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions