Skip to content

Commit 196b652

Browse files
authored
Merge pull request #81 from matthewfahrenkrug/qemufwcfg
core: Return valid values from next_file_selector
2 parents 4cb31cd + b2b203d commit 196b652

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

mythril_core/src/device/qemu_fw_cfg.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use bitflags::bitflags;
1313
use core::convert::TryInto;
1414

1515
// This is _almost_ an enum, but there are 'file' selectors
16-
// between 0x20 and 0x7fff that make it impractical to actually
16+
// between 0x20 and 0x7fff inclusive that make it impractical to actually
1717
// enumerate the selectors.
1818
#[allow(non_snake_case)]
1919
pub mod FwCfgSelector {
@@ -174,12 +174,14 @@ impl QemuFwCfgBuilder {
174174
}
175175

176176
fn next_file_selector(&self) -> u16 {
177-
//TODO: this should only consider keys below 0x8000
178177
self.data
179178
.keys()
180179
.copied()
180+
.filter(|&s| {
181+
s >= FwCfgSelector::FILE_FIRST && s <= FwCfgSelector::FILE_LAST
182+
})
181183
.max()
182-
.unwrap_or(FwCfgSelector::FILE_FIRST)
184+
.unwrap_or(FwCfgSelector::FILE_FIRST - 1)
183185
+ 1
184186
}
185187

@@ -396,3 +398,25 @@ impl EmulatedDevice for QemuFwCfg {
396398
Ok(())
397399
}
398400
}
401+
402+
#[cfg(test)]
403+
mod test {
404+
use super::*;
405+
406+
#[test]
407+
fn test_next_file_selector_first() {
408+
let builder = QemuFwCfgBuilder::new();
409+
let selector = builder.next_file_selector();
410+
assert!(selector >= FwCfgSelector::FILE_FIRST);
411+
assert!(selector <= FwCfgSelector::FILE_LAST);
412+
}
413+
414+
#[test]
415+
fn test_next_file_selector_last() {
416+
let mut builder = QemuFwCfgBuilder::new();
417+
builder.add_i32(FwCfgSelector::FILE_LAST + 1, 0x0);
418+
let selector = builder.next_file_selector();
419+
assert!(selector >= FwCfgSelector::FILE_FIRST);
420+
assert!(selector <= FwCfgSelector::FILE_LAST);
421+
}
422+
}

0 commit comments

Comments
 (0)