Skip to content

Commit

Permalink
Merge pull request #756 from epage/opt
Browse files Browse the repository at this point in the history
refactor(fuzz): Reject invalid UTF-8 data
  • Loading branch information
epage authored Jul 15, 2024
2 parents 9261857 + c0a772c commit cd9321f
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions crates/toml_edit_fuzz/parse_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

use toml_edit::DocumentMut;

libfuzzer_sys::fuzz_target!(|data| {
if let Ok(data) = std::str::from_utf8(data) {
println!("parsing: {data:?}");
let doc = match data.parse::<DocumentMut>() {
Ok(doc) => doc,
Err(err) => {
println!("{err}");
return;
}
};
let toml = doc.to_string();
println!("parsing: {toml:?}");
let doc = toml.parse::<DocumentMut>();
assert!(
doc.is_ok(),
"Failed to parse `doc.to_string()`: {}\n```\n{}\n```",
doc.unwrap_err(),
toml
);
let doc = doc.unwrap();
assert_eq!(doc.to_string(), toml);
}
libfuzzer_sys::fuzz_target!(|data: &[u8]| -> libfuzzer_sys::Corpus {
let Ok(data) = std::str::from_utf8(data) else {
return libfuzzer_sys::Corpus::Reject;
};

println!("parsing: {data:?}");
let doc = match data.parse::<DocumentMut>() {
Ok(doc) => doc,
Err(err) => {
println!("{err}");
return libfuzzer_sys::Corpus::Keep;
}
};
let toml = doc.to_string();
println!("parsing: {toml:?}");
let doc = toml.parse::<DocumentMut>();
assert!(
doc.is_ok(),
"Failed to parse `doc.to_string()`: {}\n```\n{}\n```",
doc.unwrap_err(),
toml
);
let doc = doc.unwrap();
assert_eq!(doc.to_string(), toml);
libfuzzer_sys::Corpus::Keep
});

0 comments on commit cd9321f

Please sign in to comment.