Skip to content

Commit

Permalink
read: fix parse for empty DWP index (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
philipc committed Apr 3, 2024
1 parent 7294ff0 commit be18b8b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions crates/examples/src/bin/dwarfdump.rs
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
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

0 comments on commit be18b8b

Please sign in to comment.