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 15, 2020
1 parent 895fb64 commit 77dd16b
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/read/abbrev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 77dd16b

Please sign in to comment.