m4b / goblin

An impish, cross-platform binary parsing crate, written in Rust
MIT License
1.2k stars 161 forks source link

Replace unchecked unsafe slicing with regular slicing in elf test #262

Closed nico-abram closed 3 years ago

nico-abram commented 3 years ago

This test performs the equivalent of subslicing a slice through unsafe code and avoids the bounds checking:

    let hashtab: &[u8] = unsafe {
        let addr = base.as_ptr().add(hash_section.sh_offset as usize);
        let size = hash_section.sh_size as usize;
        slice::from_raw_parts(addr, size)
    };

Since it's a test, I expect the bounds checking to not matter (I also expect that any potential overhead is dwarfed by everything else this function is doing like the vec allocation or the Elf parsing)