From 6ff8d002f71078633d5f1476bfeaf6f2f36f52ee Mon Sep 17 00:00:00 2001 From: Aetias <144526980+AetiasHax@users.noreply.github.com> Date: Sat, 19 Oct 2024 17:39:18 +0200 Subject: [PATCH] Fix panic when parsing DWARF 2 line info for empty section (#125) * Fix panic when parsing DWARF 2 line info for empty section * Fix panic when parsing DWARF 2 line info for empty section May as well remove both unwraps :p --- objdiff-core/src/obj/read.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/objdiff-core/src/obj/read.rs b/objdiff-core/src/obj/read.rs index 2cd7f23..dad6427 100644 --- a/objdiff-core/src/obj/read.rs +++ b/objdiff-core/src/obj/read.rs @@ -431,9 +431,9 @@ fn line_info(obj_file: &File<'_>, sections: &mut [ObjSection], obj_data: &[u8]) let mut text_sections = obj_file.sections().filter(|s| s.kind() == SectionKind::Text); let section_index = text_sections.next().map(|s| s.index().0); - let mut lines = section_index.map(|index| { - &mut sections.iter_mut().find(|s| s.orig_index == index).unwrap().line_info - }); + let mut lines = section_index + .and_then(|index| sections.iter_mut().find(|s| s.orig_index == index)) + .map(|s| &mut s.line_info); let mut rows = program.rows(); while let Some((_header, row)) = rows.next_row()? { @@ -444,13 +444,9 @@ fn line_info(obj_file: &File<'_>, sections: &mut [ObjSection], obj_data: &[u8]) // The next row is the start of a new sequence, which means we must // advance to the next .text section. let section_index = text_sections.next().map(|s| s.index().0); - lines = section_index.map(|index| { - &mut sections - .iter_mut() - .find(|s| s.orig_index == index) - .unwrap() - .line_info - }); + lines = section_index + .and_then(|index| sections.iter_mut().find(|s| s.orig_index == index)) + .map(|s| &mut s.line_info); } } }