File: fuzz_date.rs

package info (click to toggle)
rust-coreutils 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 505,620 kB
  • sloc: ansic: 103,594; asm: 28,570; sh: 8,910; python: 5,581; makefile: 472; cpp: 97; javascript: 72
file content (47 lines) | stat: -rw-r--r-- 1,576 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

#![no_main]

use libfuzzer_sys::fuzz_target;

use std::ffi::OsString;
use uu_date::uumain;
use uufuzz::generate_and_run_uumain;

fuzz_target!(|data: &[u8]| {
    let delim: u8 = 0; // Null byte
    let fuzz_args: Vec<OsString> = data
        .split(|b| *b == delim)
        .filter_map(|e| std::str::from_utf8(e).ok())
        .map(OsString::from)
        .collect();

    // Skip test cases that would cause the program to read from stdin
    // These would hang the fuzzer waiting for input
    for i in 0..fuzz_args.len() {
        if let Some(arg) = fuzz_args.get(i) {
            let arg_str = arg.to_string_lossy();
            // Skip if -f- or --file=- or combined options like -Rf- (reads dates from stdin)
            if (arg_str.starts_with('-') && !arg_str.starts_with("--") && arg_str.ends_with("f-"))
                || (arg_str == "-f"
                    && fuzz_args
                        .get(i + 1)
                        .map(|a| a.to_string_lossy() == "-")
                        .unwrap_or(false))
                || arg_str == "-f-"
                || arg_str == "--file=-"
            {
                return;
            }
        }
    }

    // Add program name as first argument (required for proper argument parsing)
    let mut args = vec![OsString::from("date")];
    args.extend(fuzz_args);

    let _ = generate_and_run_uumain(&args, uumain, None);
});