Skip to content

Commit

Permalink
Note::name strips all trailing null bytes
Browse files Browse the repository at this point in the history
Add Note::name_bytes which returns the entire content of the name field.
  • Loading branch information
tamird committed May 30, 2023
1 parent a082232 commit 2970971
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/read/elf/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,24 @@ impl<'data, Elf: FileHeader> Note<'data, Elf> {
self.header.n_descsz(endian)
}

/// Return the bytes for the name field following the `NoteHeader`,
/// excluding any null terminator.
/// Return the bytes for the name field following the `NoteHeader`.
///
/// This field is usually a string including a null terminator
/// This field is usually a string including one or more trailing null bytes
/// (but it is not required to be).
///
/// The length of this field (including any null terminator) is given by
/// `n_namesz`.
/// The length of this field is given by `n_namesz`.
pub fn name_bytes(&self) -> &'data [u8] {
self.name
}

/// Return the bytes for the name field following the `NoteHeader`,
/// excluding all trailing null bytes.
pub fn name(&self) -> &'data [u8] {
if let Some((last, name)) = self.name.split_last() {
if *last == 0 {
return name;
}
let mut name = self.name;
while let [rest @ .., 0] = name {
name = rest;
}
self.name
name
}

/// Return the bytes for the desc field following the `NoteHeader`.
Expand Down

0 comments on commit 2970971

Please sign in to comment.