Debian Codegraph @

fuzz/fuzz_targets/fuzz_date.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 #![no_main]
7
8 use libfuzzer_sys::fuzz_target;
9
10 use std::ffi::OsString;
11 use uu_date::uumain;
12 use uufuzz::generate_and_run_uumain;
13
14 fuzz_target!(|data: &[u8]| {
15 let delim: u8 = 0; // Null byte
16 let fuzz_args: Vec<OsString> = data
17 .split(|b| *b == delim)
18 .filter_map(|e| std::str::from_utf8(e).ok())
19 .map(OsString::from)
20 .collect();
21
22 // Skip test cases that would cause the program to read from stdin
23 // These would hang the fuzzer waiting for input
24 for i in 0..fuzz_args.len() {
25 if let Some(arg) = fuzz_args.get(i) {
26 let arg_bytes = arg.as_encoded_bytes();
27 // Skip if -f- or --file=- or combined options like -Rf- (reads dates from stdin)
28 if (arg_bytes.first() == Some(&b'-')
29 && !arg_bytes.starts_with(b"--")
30 && arg_bytes.ends_with(b"f-"))
31 || (arg_bytes == b"-f"
32 && fuzz_args
33 .get(i + 1)
34 .map(|a| a.as_encoded_bytes() == b"-")
35 .unwrap_or(false))
36 || matches!(arg_bytes, b"-f-" | b"--file=-")
37 {
38 return;
39 }
40 }
41 }
42
43 // Add program name as first argument (required for proper argument parsing)
44 let mut args = vec![OsString::from("date")];
45 args.extend(fuzz_args);
46
47 let _ = generate_and_run_uumain(&args, uumain, None);
48 });