Skip to content

Unsoundness in read_data #21

Description

@lwz23

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions