Debian Codegraph @

fuzz/fuzz_targets/fuzz_seq.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 // spell-checker:ignore parens
7
8 #![no_main]
9 use libfuzzer_sys::fuzz_target;
10 use uu_seq::uumain;
11
12 use rand::RngExt;
13 use std::ffi::OsString;
14
15 use uufuzz::CommandResult;
16 use uufuzz::{compare_result, generate_and_run_uumain, generate_random_string, run_gnu_cmd};
17 static CMD_PATH: &str = "seq";
18
19 fn generate_seq() -> String {
20 let mut rng = rand::rng();
21
22 // Generate 1 to 3 numbers for seq arguments
23 let arg_count = rng.random_range(1..=3);
24 let mut args = Vec::new();
25
26 for _ in 0..arg_count {
27 if rng.random_ratio(1, 100) {
28 // 1% chance to add a random string
29 args.push(generate_random_string(rng.random_range(1..=10)));
30 } else {
31 // 99% chance to add a numeric value
32 match rng.random_range(0..=3) {
33 0 => args.push(rng.random_range(-10000..=10000).to_string()), // Large or small integers
34 1 => args.push(rng.random_range(-100.0..100.0).to_string()), // Floating-point numbers
35 2 => args.push(rng.random_range(-100..0).to_string()), // Negative integers
36 _ => args.push(rng.random_range(1..=100).to_string()), // Regular integers
37 }
38 }
39 }
40
41 args.join(" ")
42 }
43
44 fuzz_target!(|_data: &[u8]| {
45 let seq = generate_seq();
46 let mut args = vec![OsString::from("seq")];
47 args.extend(seq.split_whitespace().map(OsString::from));
48
49 let rust_result = generate_and_run_uumain(&args, uumain, None);
50
51 let gnu_result = match run_gnu_cmd(CMD_PATH, &args[1..], false, None) {
52 Ok(result) => result,
53 Err(error_result) => {
54 eprintln!("Failed to run GNU command:");
55 eprintln!("Stderr: {}", error_result.stderr);
56 eprintln!("Exit Code: {}", error_result.exit_code);
57 CommandResult {
58 stdout: String::new(),
59 stderr: error_result.stderr,
60 exit_code: error_result.exit_code,
61 }
62 }
63 };
64
65 compare_result(
66 "seq",
67 &format!("{:?}", &args[1..]),
68 None,
69 &rust_result,
70 &gnu_result,
71 false, // Set to true if you want to fail on stderr diff
72 );
73 });