m4b / goblin

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

PE strings include NUL char #311

Open shuffle2 opened 2 years ago

shuffle2 commented 2 years ago

i'm using goblin to get the required info from PE headers to construct path of pdb on symbol server. cv.filename retains the NUL terminator and was screwing stuff up. It might be annoying to implement correctly throughout the parser, as iirc some strings are exactly sized while others still keep terminating char (even tho they're sized), and probably others can be either way. It would be great if goblin presented them in a uniform way.

if let Some(cv) = debug.codeview_pdb70_debug_info {
    if cv.filename.len() > 0 && cv.filename[0] == 0 {
        // goblin keeps NUL in str :/
        return None;
    }
    if let Ok(pdb_path) =
        std::str::from_utf8(&cv.filename[..cv.filename.len() - 1])
    {
        if let Some(pdb_name) =
            Path::new(pdb_path).file_name().and_then(|x| x.to_str())
        {
            if let Ok(guid) = uuid::Uuid::from_slice_le(&cv.signature) {
                let specifier =
                    format!("{}{:X}", guid.simple().to_string(), cv.age);
                return Some(format!(
                    "{}/{}/{}",
                    pdb_name, specifier, pdb_name
                ));
            }
        }
    }
}
m4b commented 2 years ago

@shuffle2 would you like to investigate this issue/send a PR? :)