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 18, 2020
1 parent 895fb64 commit b4857f4
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 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::TryFrom;
use core::fmt::{self, Debug};
use core::iter::FromIterator;
use core::ops::Deref;
Expand Down Expand Up @@ -154,12 +155,14 @@ 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])
} else {
self.map.get(&code)
if let Ok(code) = usize::try_from(code) {
let index = code.checked_sub(1)?;
if index < self.vec.len() {
return Some(&self.vec[index]);
}
}

self.map.get(&code)
}

/// Parse a series of abbreviations, terminated by a null abbreviation.
Expand Down Expand Up @@ -945,4 +948,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 b4857f4

Please sign in to comment.