Skip to content

Commit 5c19f68

Browse files
committed
Reword read error messages
Try to make errors more concise and consistent, and report values where possible.
1 parent 96dd55f commit 5c19f68

File tree

13 files changed

+225
-300
lines changed

13 files changed

+225
-300
lines changed

src/read/abbrev.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,9 @@ impl Abbreviations {
267267
let mut abbrevs = Abbreviations::empty();
268268

269269
while let Some(abbrev) = Abbreviation::parse(input)? {
270+
let code = abbrev.code;
270271
if abbrevs.insert(abbrev).is_err() {
271-
return Err(Error::DuplicateAbbreviationCode);
272+
return Err(Error::DuplicateAbbreviationCode(code));
272273
}
273274
}
274275

@@ -348,7 +349,7 @@ impl Abbreviation {
348349
if val == constants::DW_CHILDREN_no || val == constants::DW_CHILDREN_yes {
349350
Ok(val)
350351
} else {
351-
Err(Error::BadHasChildren)
352+
Err(Error::InvalidAbbreviationChildren(val))
352353
}
353354
}
354355

@@ -894,7 +895,7 @@ pub(crate) mod tests {
894895
let buf = &mut EndianSlice::new(&buf, LittleEndian);
895896

896897
match Abbreviations::parse(buf) {
897-
Err(Error::DuplicateAbbreviationCode) => {}
898+
Err(Error::DuplicateAbbreviationCode(1)) => {}
898899
otherwise => panic!("Unexpected result: {:?}", otherwise),
899900
};
900901
}
@@ -927,7 +928,7 @@ pub(crate) mod tests {
927928
let val = Abbreviation::parse_has_children(rest).expect("Should parse children");
928929
assert_eq!(val, constants::DW_CHILDREN_yes);
929930
match Abbreviation::parse_has_children(rest) {
930-
Err(Error::BadHasChildren) => {}
931+
Err(Error::InvalidAbbreviationChildren(constants::DwChildren(2))) => {}
931932
otherwise => panic!("Unexpected result: {:?}", otherwise),
932933
};
933934
}

src/read/addr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ where
156156
let address_size = rest.read_address_size()?;
157157
let segment_size = rest.read_u8()?;
158158
if segment_size != 0 {
159-
return Err(Error::UnsupportedSegmentSize);
159+
return Err(Error::UnsupportedSegmentSize(segment_size));
160160
}
161161

162162
// unit_length + version + address_size + segment_size

src/read/aranges.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ where
170170
let address_size = rest.read_address_size()?;
171171
let segment_size = rest.read_u8()?;
172172
if segment_size != 0 {
173-
return Err(Error::UnsupportedSegmentSize);
173+
return Err(Error::UnsupportedSegmentSize(segment_size));
174174
}
175175

176176
// unit_length + version + offset + address_size + segment_size

src/read/cfi.rs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<R: Reader> EhFrameHdr<R> {
161161
fde_count = 0
162162
} else {
163163
if fde_count_enc != fde_count_enc.format() {
164-
return Err(Error::UnsupportedPointerEncoding);
164+
return Err(Error::UnsupportedPointerEncoding(fde_count_enc));
165165
}
166166
fde_count = parse_encoded_value(fde_count_enc, &parameters, &mut reader)?;
167167
}
@@ -256,13 +256,10 @@ impl<'a, 'bases, R: Reader> EhHdrTableIter<'a, 'bases, R> {
256256
pub fn nth(&mut self, n: usize) -> Result<Option<(Pointer, Pointer)>> {
257257
use core::convert::TryFrom;
258258
let size = match self.hdr.table_enc.format() {
259-
constants::DW_EH_PE_uleb128 | constants::DW_EH_PE_sleb128 => {
260-
return Err(Error::VariableLengthSearchTable);
261-
}
262259
constants::DW_EH_PE_sdata2 | constants::DW_EH_PE_udata2 => 2,
263260
constants::DW_EH_PE_sdata4 | constants::DW_EH_PE_udata4 => 4,
264261
constants::DW_EH_PE_sdata8 | constants::DW_EH_PE_udata8 => 8,
265-
_ => return Err(Error::UnknownPointerEncoding(self.hdr.table_enc)),
262+
_ => return Err(Error::UnsupportedPointerEncoding(self.hdr.table_enc)),
266263
};
267264

268265
let row_size = size * 2;
@@ -343,13 +340,10 @@ impl<'a, R: Reader + 'a> EhHdrTable<'a, R> {
343340
/// To be sure, you **must** call `contains` on the FDE.
344341
pub fn lookup(&self, address: u64, bases: &BaseAddresses) -> Result<Pointer> {
345342
let size = match self.hdr.table_enc.format() {
346-
constants::DW_EH_PE_uleb128 | constants::DW_EH_PE_sleb128 => {
347-
return Err(Error::VariableLengthSearchTable);
348-
}
349343
constants::DW_EH_PE_sdata2 | constants::DW_EH_PE_udata2 => 2,
350344
constants::DW_EH_PE_sdata4 | constants::DW_EH_PE_udata4 => 4,
351345
constants::DW_EH_PE_sdata8 | constants::DW_EH_PE_udata8 => 8,
352-
_ => return Err(Error::UnknownPointerEncoding(self.hdr.table_enc)),
346+
_ => return Err(Error::UnsupportedPointerEncoding(self.hdr.table_enc)),
353347
};
354348

355349
let row_size = size * 2;
@@ -656,7 +650,7 @@ pub trait UnwindSection<R: Reader>: Clone + Debug + _UnwindSectionPrivate<R> {
656650
let offset = UnwindOffset::into(offset);
657651
let input = &mut self.section().clone();
658652
input.skip(offset)?;
659-
CommonInformationEntry::parse(self, bases, input)
653+
CommonInformationEntry::parse(self, bases, input, offset)
660654
}
661655

662656
/// Parse the `PartialFrameDescriptionEntry` at the given offset.
@@ -668,7 +662,7 @@ pub trait UnwindSection<R: Reader>: Clone + Debug + _UnwindSectionPrivate<R> {
668662
let offset = UnwindOffset::into(offset);
669663
let input = &mut self.section().clone();
670664
input.skip(offset)?;
671-
PartialFrameDescriptionEntry::parse_partial(self, bases, input)
665+
PartialFrameDescriptionEntry::parse_partial(self, bases, input, offset)
672666
}
673667

674668
/// Parse the `FrameDescriptionEntry` at the given offset.
@@ -1337,12 +1331,13 @@ impl<R: Reader> CommonInformationEntry<R> {
13371331
section: &Section,
13381332
bases: &BaseAddresses,
13391333
input: &mut R,
1334+
offset: R::Offset,
13401335
) -> Result<CommonInformationEntry<R>> {
13411336
let Some(prefix) = parse_cfi_entry_prefix(section, input)? else {
1342-
return Err(Error::NoEntryAtGivenOffset);
1337+
return Err(Error::NoEntryAtGivenOffset(offset.into_u64()));
13431338
};
13441339
if !Section::is_cie(prefix.format, prefix.cie_id_or_offset) {
1345-
return Err(Error::NotCieId);
1340+
return Err(Error::NotCieId(offset.into_u64()));
13461341
}
13471342
CommonInformationEntry::from_prefix(section, bases, prefix)
13481343
}
@@ -1369,7 +1364,7 @@ impl<R: Reader> CommonInformationEntry<R> {
13691364
let address_size = rest.read_address_size()?;
13701365
let segment_size = rest.read_u8()?;
13711366
if segment_size != 0 {
1372-
return Err(Error::UnsupportedSegmentSize);
1367+
return Err(Error::UnsupportedSegmentSize(segment_size));
13731368
}
13741369
address_size
13751370
} else {
@@ -1564,12 +1559,13 @@ where
15641559
section: &Section,
15651560
bases: &'bases BaseAddresses,
15661561
input: &mut R,
1562+
offset: R::Offset,
15671563
) -> Result<PartialFrameDescriptionEntry<'bases, Section, R>> {
15681564
let Some(prefix) = parse_cfi_entry_prefix(section, input)? else {
1569-
return Err(Error::NoEntryAtGivenOffset);
1565+
return Err(Error::NoEntryAtGivenOffset(offset.into_u64()));
15701566
};
15711567
if Section::is_cie(prefix.format, prefix.cie_id_or_offset) {
1572-
return Err(Error::NotCiePointer);
1568+
return Err(Error::NotCiePointer(offset.into_u64()));
15731569
}
15741570
Self::from_prefix(section, bases, prefix)
15751571
}
@@ -1582,7 +1578,7 @@ where
15821578
let cie_offset = R::Offset::from_u64(prefix.cie_id_or_offset)?;
15831579
let Some(cie_offset) = section.resolve_cie_offset(prefix.cie_offset_base, cie_offset)
15841580
else {
1585-
return Err(Error::OffsetOutOfBounds);
1581+
return Err(Error::OffsetOutOfBounds(cie_offset.into_u64()));
15861582
};
15871583

15881584
let fde = PartialFrameDescriptionEntry {
@@ -2371,7 +2367,7 @@ where
23712367
// address for the next row.
23722368
SetLoc { address } => {
23732369
if address < self.ctx.start_address() {
2374-
return Err(Error::InvalidAddressRange);
2370+
return Err(Error::InvalidCfiSetLoc(address));
23752371
}
23762372

23772373
self.next_start_address = address;
@@ -3650,7 +3646,7 @@ impl Pointer {
36503646
pub fn direct(self) -> Result<u64> {
36513647
match self {
36523648
Pointer::Direct(p) => Ok(p),
3653-
Pointer::Indirect(_) => Err(Error::UnsupportedPointerEncoding),
3649+
Pointer::Indirect(_) => Err(Error::UnsupportedIndirectPointer),
36543650
}
36553651
}
36563652

@@ -3717,7 +3713,7 @@ fn parse_encoded_pointer<R: Reader>(
37173713
return Err(Error::FuncRelativePointerInBadContext);
37183714
}
37193715
}
3720-
constants::DW_EH_PE_aligned => return Err(Error::UnsupportedPointerEncoding),
3716+
constants::DW_EH_PE_aligned => return Err(Error::UnsupportedPointerEncoding(encoding)),
37213717
_ => unreachable!(),
37223718
};
37233719

@@ -3826,7 +3822,7 @@ mod tests {
38263822
let bases = Default::default();
38273823
match parse_cfi_entry(&section, &bases, input) {
38283824
Ok(Some(CieOrFde::Fde(partial))) => partial.parse(get_cie),
3829-
Ok(_) => Err(Error::NoEntryAtGivenOffset),
3825+
Ok(_) => Err(Error::NoEntryAtGivenOffset(0)),
38303826
Err(e) => Err(e),
38313827
}
38323828
}
@@ -4030,7 +4026,7 @@ mod tests {
40304026
debug_frame.set_address_size(address_size);
40314027
let input = &mut EndianSlice::new(&section, E::default());
40324028
let bases = Default::default();
4033-
let result = CommonInformationEntry::parse(&debug_frame, &bases, input);
4029+
let result = CommonInformationEntry::parse(&debug_frame, &bases, input, 0);
40344030
let result = result.map(|cie| (*input, cie)).map_eof(&section);
40354031
assert_eq!(result, expected);
40364032
}
@@ -4084,7 +4080,7 @@ mod tests {
40844080
.B32(4)
40854081
// Not the CIE Id.
40864082
.B32(0xbad1_bad2);
4087-
assert_parse_cie(kind, section, 8, Err(Error::NotCieId));
4083+
assert_parse_cie(kind, section, 8, Err(Error::NotCieId(0)));
40884084
}
40894085

40904086
#[test]
@@ -4225,7 +4221,8 @@ mod tests {
42254221
CommonInformationEntry::parse(
42264222
&debug_frame,
42274223
&bases,
4228-
&mut EndianSlice::new(&contents, LittleEndian)
4224+
&mut EndianSlice::new(&contents, LittleEndian),
4225+
0,
42294226
)
42304227
.map_eof(&contents),
42314228
Err(Error::UnexpectedEof(ReaderOffsetId(4)))
@@ -5404,7 +5401,7 @@ mod tests {
54045401
ctx.row_mut().start_address = 999;
54055402
let expected = ctx.clone();
54065403
let instructions = [(
5407-
Err(Error::InvalidAddressRange),
5404+
Err(Error::InvalidCfiSetLoc(42)),
54085405
CallFrameInstruction::SetLoc { address: 42 },
54095406
)];
54105407
assert_eval(ctx, expected, cie, None, instructions);
@@ -6500,7 +6497,9 @@ mod tests {
65006497
let table = table.unwrap();
65016498
assert_eq!(
65026499
table.lookup(0, &bases),
6503-
Err(Error::VariableLengthSearchTable)
6500+
Err(Error::UnsupportedPointerEncoding(
6501+
constants::DW_EH_PE_uleb128
6502+
))
65046503
);
65056504
}
65066505

@@ -6517,7 +6516,12 @@ mod tests {
65176516
let bases = BaseAddresses::default();
65186517
let result = EhFrameHdr::new(&section, LittleEndian).parse(&bases, 8);
65196518
assert!(result.is_err());
6520-
assert_eq!(result.unwrap_err(), Error::UnsupportedPointerEncoding);
6519+
assert_eq!(
6520+
result.unwrap_err(),
6521+
Error::UnsupportedPointerEncoding(
6522+
constants::DW_EH_PE_indirect | constants::DW_EH_PE_udata4
6523+
)
6524+
);
65216525
}
65226526

65236527
#[test]
@@ -6544,7 +6548,7 @@ mod tests {
65446548
let table = table.unwrap();
65456549
assert_eq!(
65466550
table.lookup(0, &bases),
6547-
Err(Error::UnsupportedPointerEncoding)
6551+
Err(Error::UnsupportedIndirectPointer)
65486552
);
65496553
}
65506554

@@ -6737,7 +6741,7 @@ mod tests {
67376741

67386742
assert_eq!(
67396743
eh_frame.cie_from_offset(&bases, EhFrameOffset(0)),
6740-
Err(Error::NoEntryAtGivenOffset)
6744+
Err(Error::NoEntryAtGivenOffset(0))
67416745
);
67426746
}
67436747

@@ -6759,7 +6763,7 @@ mod tests {
67596763

67606764
assert_eq!(
67616765
debug_frame.cie_from_offset(&bases, DebugFrameOffset(0)),
6762-
Err(Error::NoEntryAtGivenOffset)
6766+
Err(Error::NoEntryAtGivenOffset(0))
67636767
);
67646768
}
67656769

@@ -6809,7 +6813,7 @@ mod tests {
68096813
let buf = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
68106814
assert_eq!(
68116815
resolve_cie_offset(&buf, buf.len() + 4 + 2),
6812-
Err(Error::OffsetOutOfBounds)
6816+
Err(Error::OffsetOutOfBounds(buf.len() as u64 + 4 + 2))
68136817
);
68146818
}
68156819

@@ -6818,7 +6822,7 @@ mod tests {
68186822
let buf = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
68196823
assert_eq!(
68206824
resolve_cie_offset(&buf, usize::MAX),
6821-
Err(Error::OffsetOutOfBounds)
6825+
Err(Error::OffsetOutOfBounds(u32::MAX as u64))
68226826
);
68236827
}
68246828

@@ -6899,7 +6903,7 @@ mod tests {
68996903
let section = Section::with_endian(kind.endian())
69006904
.cie(kind, None, &mut cie)
69016905
.mark(&end_of_cie)
6902-
.fde(kind, 99_999_999_999_999, &mut fde);
6906+
.fde(kind, 99_999_999, &mut fde);
69036907

69046908
section.start().set_const(0);
69056909
let section = section.get_contents().unwrap();
@@ -6911,7 +6915,7 @@ mod tests {
69116915
&mut section.range_from(end_of_cie.value().unwrap() as usize..),
69126916
UnwindSection::cie_from_offset,
69136917
);
6914-
assert_eq!(result, Err(Error::OffsetOutOfBounds));
6918+
assert_eq!(result, Err(Error::OffsetOutOfBounds(99_999_999)));
69156919
}
69166920

69176921
#[test]
@@ -7933,7 +7937,7 @@ mod tests {
79337937
};
79347938
assert_eq!(
79357939
parse_encoded_pointer(encoding, &parameters, &mut rest),
7936-
Err(Error::UnsupportedPointerEncoding)
7940+
Err(Error::UnsupportedPointerEncoding(encoding))
79377941
);
79387942
}
79397943

src/read/dwarf.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -605,15 +605,15 @@ impl<R: Reader> Dwarf<R> {
605605
constants::DW_AT_low_pc => {
606606
low_pc = Some(
607607
self.attr_address(unit, attr.value())?
608-
.ok_or(Error::UnsupportedAttributeForm)?,
608+
.ok_or(Error::UnsupportedAttributeForm(attr.form()))?,
609609
);
610610
}
611611
constants::DW_AT_high_pc => match attr.value() {
612612
AttributeValue::Udata(val) => size = Some(val),
613-
attr => {
613+
value => {
614614
high_pc = Some(
615-
self.attr_address(unit, attr)?
616-
.ok_or(Error::UnsupportedAttributeForm)?,
615+
self.attr_address(unit, value)?
616+
.ok_or(Error::UnsupportedAttributeForm(attr.form()))?,
617617
);
618618
}
619619
},
@@ -1725,7 +1725,7 @@ mod tests {
17251725
Err(e) => {
17261726
assert_eq!(
17271727
dwarf.format_error(e),
1728-
"Hit the end of input before it was expected at .debug_str+0x1"
1728+
"unexpected end of input at .debug_str+0x1"
17291729
);
17301730
}
17311731
}
@@ -1734,7 +1734,7 @@ mod tests {
17341734
Err(e) => {
17351735
assert_eq!(
17361736
dwarf.format_error(e),
1737-
"Hit the end of input before it was expected at .debug_str(sup)+0x1"
1737+
"unexpected end of input at .debug_str(sup)+0x1"
17381738
);
17391739
}
17401740
}

0 commit comments

Comments
 (0)