Skip to content

Commit

Permalink
read: Allow section names to be borrowed for 'data (gimli-rs#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlattimore authored Jan 6, 2024
1 parent a45df14 commit 5ed30c2
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/read/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,11 +756,11 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSection<'data> for Section<'data, 'f
with_inner!(self.inner, SectionInternal, |x| x.compressed_data())
}

fn name_bytes(&self) -> Result<&[u8]> {
fn name_bytes(&self) -> Result<&'data [u8]> {
with_inner!(self.inner, SectionInternal, |x| x.name_bytes())
}

fn name(&self) -> Result<&str> {
fn name(&self) -> Result<&'data str> {
with_inner!(self.inner, SectionInternal, |x| x.name())
}

Expand Down
4 changes: 2 additions & 2 deletions src/read/coff/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> ObjectSection<'data>
}

#[inline]
fn name_bytes(&self) -> Result<&[u8]> {
fn name_bytes(&self) -> Result<&'data [u8]> {
self.section.name(self.file.common.symbols.strings())
}

#[inline]
fn name(&self) -> Result<&str> {
fn name(&self) -> Result<&'data str> {
let name = self.name_bytes()?;
str::from_utf8(name)
.ok()
Expand Down
4 changes: 2 additions & 2 deletions src/read/elf/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,13 @@ where
self.compressed_file_range()?.data(self.file.data)
}

fn name_bytes(&self) -> read::Result<&[u8]> {
fn name_bytes(&self) -> read::Result<&'data [u8]> {
self.file
.sections
.section_name(self.file.endian, self.section)
}

fn name(&self) -> read::Result<&str> {
fn name(&self) -> read::Result<&'data str> {
let name = self.name_bytes()?;
str::from_utf8(name)
.ok()
Expand Down
4 changes: 2 additions & 2 deletions src/read/macho/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ where
}

#[inline]
fn name_bytes(&self) -> Result<&[u8]> {
fn name_bytes(&self) -> Result<&'data [u8]> {
Ok(self.internal.section.name())
}

#[inline]
fn name(&self) -> Result<&str> {
fn name(&self) -> Result<&'data str> {
str::from_utf8(self.internal.section.name())
.ok()
.read_error("Non UTF-8 Mach-O section name")
Expand Down
4 changes: 2 additions & 2 deletions src/read/pe/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,12 @@ where
}

#[inline]
fn name_bytes(&self) -> Result<&[u8]> {
fn name_bytes(&self) -> Result<&'data [u8]> {
self.section.name(self.file.common.symbols.strings())
}

#[inline]
fn name(&self) -> Result<&str> {
fn name(&self) -> Result<&'data str> {
let name = self.name_bytes()?;
str::from_utf8(name)
.ok()
Expand Down
4 changes: 2 additions & 2 deletions src/read/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,12 @@ pub trait ObjectSection<'data>: read::private::Sealed {
}

/// Returns the name of the section.
fn name_bytes(&self) -> Result<&[u8]>;
fn name_bytes(&self) -> Result<&'data [u8]>;

/// Returns the name of the section.
///
/// Returns an error if the name is not UTF-8.
fn name(&self) -> Result<&str>;
fn name(&self) -> Result<&'data str>;

/// Returns the name of the segment for this section.
fn segment_name_bytes(&self) -> Result<Option<&[u8]>>;
Expand Down
4 changes: 2 additions & 2 deletions src/read/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,12 +663,12 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSection<'data> for WasmSection<'data
}

#[inline]
fn name_bytes(&self) -> Result<&[u8]> {
fn name_bytes(&self) -> Result<&'data [u8]> {
self.name().map(str::as_bytes)
}

#[inline]
fn name(&self) -> Result<&str> {
fn name(&self) -> Result<&'data str> {
Ok(match self.section.id {
SectionId::Custom => self.section.name,
SectionId::Type => "<type>",
Expand Down
4 changes: 2 additions & 2 deletions src/read/xcoff/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ where
self.data().map(CompressedData::none)
}

fn name_bytes(&self) -> read::Result<&[u8]> {
fn name_bytes(&self) -> read::Result<&'data [u8]> {
Ok(self.section.name())
}

fn name(&self) -> read::Result<&str> {
fn name(&self) -> read::Result<&'data str> {
let name = self.name_bytes()?;
str::from_utf8(name)
.ok()
Expand Down

0 comments on commit 5ed30c2

Please sign in to comment.