Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read: Allow section names to be borrowed for 'data #620

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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