1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

fuzz: adapt to API changes of rand

This commit is contained in:
Daniel Hofstetter 2025-01-30 10:57:17 +01:00
parent 527602248f
commit 9aec9dc454
13 changed files with 115 additions and 115 deletions

View file

@ -8,7 +8,7 @@
use libfuzzer_sys::fuzz_target;
use uu_expr::uumain;
use rand::seq::SliceRandom;
use rand::prelude::IndexedRandom;
use rand::Rng;
use std::{env, ffi::OsString};
@ -20,7 +20,7 @@ use crate::fuzz_common::{
static CMD_PATH: &str = "expr";
fn generate_expr(max_depth: u32) -> String {
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
let ops = [
"+", "-", "*", "/", "%", "<", ">", "=", "&", "|", "!=", "<=", ">=", ":", "index", "length",
"substr",
@ -33,18 +33,18 @@ fn generate_expr(max_depth: u32) -> String {
while depth <= max_depth {
if last_was_operator || depth == 0 {
// Add a number
expr.push_str(&rng.gen_range(1..=100).to_string());
expr.push_str(&rng.random_range(1..=100).to_string());
last_was_operator = false;
} else {
// 90% chance to add an operator followed by a number
if rng.gen_bool(0.9) {
if rng.random_bool(0.9) {
let op = *ops.choose(&mut rng).unwrap();
expr.push_str(&format!(" {} ", op));
last_was_operator = true;
}
// 10% chance to add a random string (potentially invalid syntax)
else {
let random_str = generate_random_string(rng.gen_range(1..=10));
let random_str = generate_random_string(rng.random_range(1..=10));
expr.push_str(&random_str);
last_was_operator = false;
}
@ -54,15 +54,15 @@ fn generate_expr(max_depth: u32) -> String {
// Ensure the expression ends with a number if it ended with an operator
if last_was_operator {
expr.push_str(&rng.gen_range(1..=100).to_string());
expr.push_str(&rng.random_range(1..=100).to_string());
}
expr
}
fuzz_target!(|_data: &[u8]| {
let mut rng = rand::thread_rng();
let expr = generate_expr(rng.gen_range(0..=20));
let mut rng = rand::rng();
let expr = generate_expr(rng.random_range(0..=20));
let mut args = vec![OsString::from("expr")];
args.extend(expr.split_whitespace().map(OsString::from));