Skip to content

Maybe unsound in new? #3

@lwz23

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 new(bytes: *const [u8]) -> Result<Elf<'a>, ElfError> {
        let bytes = unsafe { &*bytes };
        for i in 0..100 {
            dprint!("{} ", bytes[i]);
            if i % 10 == 9 {
                dprintln!();
            }
        }
        let elf = unsafe {
            let data: *const ElfHeader = bytes.as_ptr() as *const ElfHeader;
            &*(data)
        };
        dprintln!("{:?}", elf);
        if elf.magic != ELF_MAGIC {
            return Err(ElfError::InvalidMagic);
        }
        Ok(Elf { bytes, elf })
    }

I think there may exist a unsound problem in this function. there is no test for the bytes, if the user pass a null pointer to this function it will lead to UB.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions