diff --git a/crates/toml_edit_fuzz/parse_document.rs b/crates/toml_edit_fuzz/parse_document.rs index 9bc0fed5..a503df41 100644 --- a/crates/toml_edit_fuzz/parse_document.rs +++ b/crates/toml_edit_fuzz/parse_document.rs @@ -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::() { - Ok(doc) => doc, - Err(err) => { - println!("{err}"); - return; - } - }; - let toml = doc.to_string(); - println!("parsing: {toml:?}"); - let doc = toml.parse::(); - 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::() { + Ok(doc) => doc, + Err(err) => { + println!("{err}"); + return libfuzzer_sys::Corpus::Keep; + } + }; + let toml = doc.to_string(); + println!("parsing: {toml:?}"); + let doc = toml.parse::(); + 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 });