Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read: Fix subtraction overflow when Abbreviations::get is given 0 #505

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}