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

Unsoundness in read_data #21

Open
lwz23 opened this issue Jan 2, 2025 · 0 comments
Open

Unsoundness in read_data #21

lwz23 opened this issue Jan 2, 2025 · 0 comments

Comments

@lwz23
Copy link

lwz23 commented Jan 2, 2025

Hello, thank you for your contribution in this project, I am scanning the unsoundness problem in rust project.
I notice the following code:

pub fn read_data(data: &mut Vec<i64>, encoded_data: &[u8], index: &mut usize) {
        let length = data.len();
        if length > 0 {
            data.clear();
            let ptr = encoded_data[*index..].as_ptr() as *mut i64;

            let v = unsafe { Vec::from_raw_parts(ptr, length, length) };
            let _ = v.iter().map(|&x| data.push(x)).collect::<()>();
            v.into_raw_parts();
            *index += length << 3;
        }
    }

This is because it converts a pointer of type 'u8' to a pointer of type 'i64', which requires 8-byte alignment, while a pointer of type 'u8' does not guarantee this alignment. This can result in undefined behavior (UB).

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