Skip to content

ChunkId::new panic with misaligned pointer dereference #275

@shinmao

Description

@shinmao

Describe the bug

pub fn new(hash_value: &HashValue, len: u32) -> Self {
let hash = hash_value.as_slice();
let mut id = Self::default();
let chunkid = id.as_mut_slice();
chunkid[0] = 0b_01000000 | (ObjectTypeCode::Chunk.to_u16() as u8) << 4 >> 2;
// chunkid[0] = ObjectTypeCode::Chunk.to_u16() as u8;
unsafe {
*(chunkid[1..5].as_mut_ptr() as *mut u32) = len;
}
chunkid[5..].copy_from_slice(&hash[0..27]);
id
}

No matter how I try to create ChunkId with ChunkId::new, it will panic and show the error: misaligned pointer dereference: address must be a multiple of 0x4 but is 0xaddress.

To Reproduce

fn main() {
    let (chunk_len, chunk_data) = random_mem(1024, 1024);
    let chunk_hash = hash_data(&chunk_data[..]);
    println!("{:?}", chunk_hash);
    let chunkid = ChunkId::new(&chunk_hash, chunk_len as u32);
}

There is no sample code in documentation; therefore, I follow the sample code and test I found in repository.

Expected behavior
I expected to get chunk_id at the last line. I am sure the bug comes from new function because I always could print chunk_hash.

System information

[dependencies]
cyfs-base = "0.6.12"
cyfs-bdt = "0.7.3"
cyfs-lib = "0.8.3"

Here are my dependencies. I used it on ubuntu 20.04 with x86-64.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions