diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index cb1951c..31c6f9b 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -18,5 +18,9 @@ git = "https://github.com/rust-fuzz/libfuzzer-sys.git" members = ["."] [[bin]] -name = "fuzz_target_1" -path = "fuzz_targets/fuzz_target_1.rs" +name = "fuzz_parse" +path = "fuzz_targets/fuzz_parse.rs" + +[[bin]] +name = "fuzz_conversion" +path = "fuzz_targets/fuzz_conversion.rs" diff --git a/fuzz/fuzz_targets/fuzz_conversion.rs b/fuzz/fuzz_targets/fuzz_conversion.rs new file mode 100644 index 0000000..b56db76 --- /dev/null +++ b/fuzz/fuzz_targets/fuzz_conversion.rs @@ -0,0 +1,51 @@ +#![no_main] + +#[macro_use] extern crate libfuzzer_sys; +extern crate httpdate; + +use httpdate::HttpDate; +use std::time::{SystemTime, UNIX_EPOCH}; +use std::convert::TryInto; + +fuzz_target!(|data: &[u8]| { + // Skip this round if data is not enough + if data.len() < 8 { + return; + } + + // Create system time object + let secs_since_epoch = u64::from_le_bytes(data[0..8].try_into().unwrap_or_default()); + let duration = std::time::Duration::from_secs(secs_since_epoch); + let system_time = match UNIX_EPOCH.checked_add(duration) { + Some(time) => time, + None => return, + }; + + // Skip value that could make HttpDate panic + if secs_since_epoch >= 253402300800 { + return; + } + + // Fuzz other functions + let http_date = HttpDate::from(system_time); + let _ = SystemTime::from(http_date); + let _ = http_date.to_string(); + + // Fuzz partial_cmp if enough data is left + if data.len() >= 16 { + let other_secs_since_epoch = u64::from_le_bytes(data[8..16].try_into().unwrap_or_default()); + let other_duration = std::time::Duration::from_secs(other_secs_since_epoch); + let other_system_time = match UNIX_EPOCH.checked_add(other_duration) { + Some(time) => time, + None => return, + }; + + // Skip value that could make HttpDate panic + if other_secs_since_epoch >= 253402300800 { + return; + } + + let other_http_date = HttpDate::from(other_system_time); + let _ = http_date.partial_cmp(&other_http_date); + } +}); diff --git a/fuzz/fuzz_targets/fuzz_target_1.rs b/fuzz/fuzz_targets/fuzz_parse.rs similarity index 100% rename from fuzz/fuzz_targets/fuzz_target_1.rs rename to fuzz/fuzz_targets/fuzz_parse.rs