diff --git a/src/read/abbrev.rs b/src/read/abbrev.rs index a5b0fb40a..fa22708e2 100644 --- a/src/read/abbrev.rs +++ b/src/read/abbrev.rs @@ -2,6 +2,7 @@ use alloc::collections::btree_map; use alloc::vec::Vec; +use core::convert::TryInto; use core::fmt::{self, Debug}; use core::iter::FromIterator; use core::ops::Deref; @@ -154,9 +155,10 @@ impl Abbreviations { /// Get the abbreviation associated with the given code. #[inline] pub fn get(&self, code: u64) -> Option<&Abbreviation> { - let code_usize = code as usize; - if code_usize as u64 == code && code_usize - 1 < self.vec.len() { - Some(&self.vec[code_usize - 1]) + let code_usize: usize = code.try_into().ok()?; + let index = code_usize.checked_sub(1)?; + if index < self.vec.len() { + Some(&self.vec[index]) } else { self.map.get(&code) } @@ -945,4 +947,18 @@ pub mod tests { otherwise => panic!("Unexpected result: {:?}", otherwise), }; } + + #[test] + fn test_get_abbrev_zero() { + let mut abbrevs = Abbreviations::empty(); + abbrevs + .insert(Abbreviation::new( + 1, + constants::DwTag(1), + constants::DW_CHILDREN_no, + vec![].into(), + )) + .unwrap(); + assert!(abbrevs.get(0).is_none()); + } }