Skip to content

Commit

Permalink
[object/fuzz] Update fuzz target open_file
Browse files Browse the repository at this point in the history
- remove group length elements before writing
- assume that writing back should not fail
- tweak target to keep corpus when the fuzz does not fail
  • Loading branch information
Enet4 committed Oct 5, 2024
1 parent d250387 commit e79199c
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions object/fuzz/fuzz_targets/open_file.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use libfuzzer_sys::{fuzz_target, Corpus};
use std::error::Error;

fuzz_target!(|data: &[u8]| {
let _ = fuzz(data);
fuzz_target!(|data: &[u8]| -> Corpus {
match fuzz(data) {
Ok(_) => Corpus::Keep,
Err(_) => Corpus::Reject,
}
});

fn fuzz(data: &[u8]) -> Result<(), Box<dyn Error>> {
// deserialize random bytes
let obj = dicom_object::from_reader(data)?;
let mut obj = dicom_object::OpenFileOptions::new()
.read_preamble(dicom_object::file::ReadPreamble::Auto)
.odd_length_strategy(dicom_object::file::OddLengthStrategy::Fail)
.from_reader(data)?;

// remove group length elements
for g in 0..=0x07FF {
obj.remove_element(dicom_object::Tag(g, 0x0000));
}
// serialize object back to bytes
let mut bytes = Vec::new();
obj.write_all(&mut bytes)?;
obj.write_all(&mut bytes)
.expect("writing DICOM file should always be successful");

// deserialize back to object
let obj2 = dicom_object::from_reader(bytes.as_slice())
Expand Down

0 comments on commit e79199c

Please sign in to comment.