aki-akaguma / memx

memory functions like a libc memcmp(), memchr(), memmem(), memcpy(), memset()
Apache License 2.0
17 stars 2 forks source link

Unsound usages of unsafe implementation from `u8` to `u128` #3

Open llooFlashooll opened 1 month ago

llooFlashooll commented 1 month ago

Hi, I am scanning the memx in the latest version with my own static analyzer tool.

Unsafe conversion found at: src/mem/mem_set.rs#L404

#[inline(always)]
fn _set_c16_uu_x1(buf_ptr: *mut u8, c16: B16Sgl) {
    let aa_ptr = buf_ptr as *mut u128;
    unsafe { aa_ptr.write_unaligned(c16.v1) };
}

This unsound implementation would create a misalignment issues if the type size of u8 is smaller than the type size of u128. write_unaligned is not a good function, at least _set_c16_uu_x1 should be annoated with unsafe.

This would potentially cause undefined behaviors in Rust. If we further manipulate the problematic converted types, it would potentially lead to different consequences such as access out-of-bound. I am reporting this issue for your attention.

aki-akaguma commented 1 month ago

thank you. I'll think about it.