Skip to content

Commit

Permalink
test(fuzz): Verify error rendering doesn't panic
Browse files Browse the repository at this point in the history
This will ensure the span we return is within valid UTF-8 byte offsets
  • Loading branch information
epage committed Apr 19, 2024
1 parent ed26084 commit 73cbee7
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions crates/toml_edit_fuzz/parse_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ use toml_edit::DocumentMut;
libfuzzer_sys::fuzz_target!(|data| {
if let Ok(data) = std::str::from_utf8(data) {
println!("parsing: {data:?}");
let doc = data.parse::<DocumentMut>();
if let Ok(doc) = doc {
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);
}
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);
}
});

0 comments on commit 73cbee7

Please sign in to comment.