Skip to content

Commit

Permalink
read: Fix subtraction overflow when Abbreviations::get is given 0
Browse files Browse the repository at this point in the history
  • Loading branch information
fitzgen committed May 14, 2020
1 parent 895fb64 commit 6755bee
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/read/abbrev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ 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 index = (code as usize).checked_sub(1)?;
if index as u64 + 1 == code && index < self.vec.len() {
Some(&self.vec[index])
} else {
self.map.get(&code)
}
Expand Down Expand Up @@ -945,4 +945,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());
}
}

0 comments on commit 6755bee

Please sign in to comment.