-
Notifications
You must be signed in to change notification settings - Fork 276
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
ChunkId::new
panic with misaligned pointer dereference
#275
Comments
I wrote a simple test like yours: #[test]
fn chunk2() {
let mut chunk_data = [0u8;1024*1024];
let chunk_len = chunk_data.len();
rand::thread_rng().fill_bytes(&mut chunk_data);
let chunk_hash = hash_data(&chunk_data);
println!("{:?}", chunk_hash);
let chunkid = ChunkId::new(&chunk_hash, chunk_len as u32);
println!("{:?}", chunkid);
} and it run successful and print correct result: HashValue: 6e9cf3599613cf699b9bb60321f51f74dd2b2b8e15132cb3c395605aedf50a27
ChunkId: "7C8WVHtUCu9ECpJbo5wwV8FBs5gcXeHjTkonE2zWb6rK"
test objects::chunk::test::chunk2 ... ok I already commit the test at e030188 and you can run it by yourself, use command |
@weiqiushi I tried to call use rand::RngCore;
use cyfs_base::{hash_data, ChunkId, HashValue};
fn main() {
let mut chunk_data = [0u8;1024*1024];
let chunk_len = chunk_data.len();
rand::thread_rng().fill_bytes(&mut chunk_data);
let chunk_hash = hash_data(&chunk_data);
println!("{:?}", chunk_hash);
let chunkid = ChunkId::new(&chunk_hash, chunk_len as u32);
println!("{:?}", chunkid);
} and run with dependencies cyfs-base = "0.6.12"
rand = "0.7" it still shows the result
The error message shows that misaligned pointer dereference occurs at this line 229: CYFS/src/component/cyfs-base/src/objects/chunk.rs Lines 228 to 230 in e030188
I consider the problem here is that casting u8 pointer to u32 pointer causes to the mis-alignment. Hope this information could help solve my error:) |
You can use ptr::write_unaligned to fix this, BUT that leaves the endian problem present. You should instead use u32::to_le_bytes and copy the bytes. |
Describe the bug
CYFS/src/component/cyfs-base/src/objects/chunk.rs
Lines 221 to 233 in 9eaf33f
No matter how I try to create
ChunkId
withChunkId::new
, it will panic and show the error: misaligned pointer dereference: address must be a multiple of 0x4 but is 0xaddress.To Reproduce
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 fromnew
function because I always could printchunk_hash
.System information
Here are my dependencies. I used it on ubuntu 20.04 with x86-64.
The text was updated successfully, but these errors were encountered: