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: fix parse for empty DWP index #706

Merged
merged 1 commit into from
Apr 3, 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 crates/examples/src/bin/dwarfdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ fn dump_dwp<R: Reader, W: Write + Send>(
where
R::Endian: Send + Sync,
{
if dwp.cu_index.unit_count() != 0 {
if dwp.cu_index.version() != 0 {
writeln!(
w,
"\n.debug_cu_index: version = {}, sections = {}, units = {}, slots = {}",
Expand All @@ -1113,7 +1113,7 @@ where
}
}

if dwp.tu_index.unit_count() != 0 {
if dwp.tu_index.version() != 0 {
writeln!(
w,
"\n.debug_tu_index: version = {}, sections = {}, units = {}, slots = {}",
Expand Down
24 changes: 22 additions & 2 deletions src/read/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl<R: Reader> UnitIndex<R> {
fn parse(mut input: R) -> Result<UnitIndex<R>> {
if input.is_empty() {
return Ok(UnitIndex {
version: 5,
version: 0,
section_count: 0,
unit_count: 0,
slot_count: 0,
Expand Down Expand Up @@ -166,7 +166,7 @@ impl<R: Reader> UnitIndex<R> {
let section_count = input.read_u32()?;
let unit_count = input.read_u32()?;
let slot_count = input.read_u32()?;
if slot_count == 0 || slot_count & (slot_count - 1) != 0 || slot_count <= unit_count {
if slot_count != 0 && (slot_count & (slot_count - 1) != 0 || slot_count <= unit_count) {
return Err(Error::InvalidIndexSlotCount);
}

Expand Down Expand Up @@ -280,6 +280,8 @@ impl<R: Reader> UnitIndex<R> {
}

/// Return the version.
///
/// Defaults to 0 for empty sections.
pub fn version(&self) -> u16 {
self.version
}
Expand Down Expand Up @@ -345,6 +347,24 @@ mod tests {
fn test_empty() {
let buf = EndianSlice::new(&[], BigEndian);
let index = UnitIndex::parse(buf).unwrap();
assert_eq!(index.version(), 0);
assert_eq!(index.unit_count(), 0);
assert_eq!(index.slot_count(), 0);
assert!(index.find(0).is_none());
}

#[test]
fn test_zero_slots() {
#[rustfmt::skip]
let section = Section::with_endian(Endian::Big)
// Header.
.D32(2).D32(0).D32(0).D32(0);
let buf = section.get_contents().unwrap();
let buf = EndianSlice::new(&buf, BigEndian);
let index = UnitIndex::parse(buf).unwrap();
assert_eq!(index.version(), 2);
assert_eq!(index.unit_count(), 0);
assert_eq!(index.slot_count(), 0);
assert!(index.find(0).is_none());
}

Expand Down
Loading