frank2 / exe-rs

The PE Executable Library, but for Rust!
GNU General Public License v3.0
71 stars 13 forks source link

get_section_by_name requres a String instead of an &str #6

Closed LunNova closed 1 year ago

LunNova commented 1 year ago
    fn get_section_by_name(&self, name: String) -> Result<&ImageSectionHeader, Error> {
        let sections = self.get_section_table()?;
        let s = name.as_str();

        for section in sections {
            if section.name.as_str() == s {
                return Ok(section);
            }
        }

        Err(Error::SectionNotFound)
    }

Taking an &str would be nicer here and it looks like it's fine since it immediately gets converted to one.

frank2 commented 1 year ago

Curse of me starting this crate when I was new to Rust, literally like confusing const char * and std::string. Thanks for reporting this, I'll get this fixed in the next version!

frank2 commented 1 year ago

This actually made me realize there's a bug here, so this might take a little longer to come out-- there's a possibility that a section name can not be a valid UTF8 string, and so the str returned by as_str() would cause a panic when the string is used because it's not valid UTF8. This happens because I'm intentionally transmuting a reference that points into the PE file.

Anyway, I've started work on this, I'll update you when I get this bug fixed!

frank2 commented 1 year ago

Okay, this issue plus the issue this uncovered have been fixed as of this commit: https://github.com/frank2/exe-rs/commit/02527d94fad138839aa28740862b32088893ee3c. This will be officially released in 0.5.5. Thanks again for reporting!

frank2 commented 1 year ago

This is live in 0.5.6 (not 0.5.5, no idea what release that was). Thanks again!