Skip to content

Commit 12d6727

Browse files
authored
Merge pull request #5524 from sylvestre/dup_fun
fuzz: Move a duplicate function into fuzz_common
2 parents 5f9f610 + fddf301 commit 12d6727

File tree

3 files changed

+31
-48
lines changed

3 files changed

+31
-48
lines changed

fuzz/fuzz_targets/fuzz_common.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// file that was distributed with this source code.
55

66
use libc::{close, dup, dup2, pipe, STDERR_FILENO, STDOUT_FILENO};
7+
use rand::prelude::SliceRandom;
8+
use rand::Rng;
79
use std::ffi::OsString;
810
use std::io;
911
use std::io::Write;
@@ -272,3 +274,26 @@ pub fn compare_result(
272274
}
273275
}
274276
}
277+
278+
pub fn generate_random_string(max_length: usize) -> String {
279+
let mut rng = rand::thread_rng();
280+
let valid_utf8: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
281+
.chars()
282+
.collect();
283+
let invalid_utf8 = [0xC3, 0x28]; // Invalid UTF-8 sequence
284+
let mut result = String::new();
285+
286+
for _ in 0..rng.gen_range(1..=max_length) {
287+
if rng.gen_bool(0.9) {
288+
let ch = valid_utf8.choose(&mut rng).unwrap();
289+
result.push(*ch);
290+
} else {
291+
let ch = invalid_utf8.choose(&mut rng).unwrap();
292+
if let Some(c) = char::from_u32(*ch as u32) {
293+
result.push(c);
294+
}
295+
}
296+
}
297+
298+
result
299+
}

fuzz/fuzz_targets/fuzz_expr.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,11 @@ use std::{env, ffi::OsString};
1414

1515
mod fuzz_common;
1616
use crate::fuzz_common::CommandResult;
17-
use crate::fuzz_common::{compare_result, generate_and_run_uumain, run_gnu_cmd};
17+
use crate::fuzz_common::{
18+
compare_result, generate_and_run_uumain, generate_random_string, run_gnu_cmd,
19+
};
1820
static CMD_PATH: &str = "expr";
1921

20-
fn generate_random_string(max_length: usize) -> String {
21-
let mut rng = rand::thread_rng();
22-
let valid_utf8: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
23-
.chars()
24-
.collect();
25-
let invalid_utf8 = [0xC3, 0x28]; // Invalid UTF-8 sequence
26-
let mut result = String::new();
27-
28-
for _ in 0..rng.gen_range(1..=max_length) {
29-
if rng.gen_bool(0.9) {
30-
let ch = valid_utf8.choose(&mut rng).unwrap();
31-
result.push(*ch);
32-
} else {
33-
let ch = invalid_utf8.choose(&mut rng).unwrap();
34-
if let Some(c) = char::from_u32(*ch as u32) {
35-
result.push(c);
36-
}
37-
}
38-
}
39-
40-
result
41-
}
42-
4322
fn generate_expr(max_depth: u32) -> String {
4423
let mut rng = rand::thread_rng();
4524
let ops = ["+", "-", "*", "/", "%", "<", ">", "=", "&", "|"];

fuzz/fuzz_targets/fuzz_test.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use std::ffi::OsString;
1414

1515
mod fuzz_common;
1616
use crate::fuzz_common::CommandResult;
17-
use crate::fuzz_common::{compare_result, generate_and_run_uumain, run_gnu_cmd};
17+
use crate::fuzz_common::{
18+
compare_result, generate_and_run_uumain, generate_random_string, run_gnu_cmd,
19+
};
1820

1921
#[derive(PartialEq, Debug, Clone)]
2022
enum ArgType {
@@ -29,29 +31,6 @@ enum ArgType {
2931

3032
static CMD_PATH: &str = "test";
3133

32-
fn generate_random_string(max_length: usize) -> String {
33-
let mut rng = rand::thread_rng();
34-
let valid_utf8: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
35-
.chars()
36-
.collect();
37-
let invalid_utf8 = [0xC3, 0x28]; // Invalid UTF-8 sequence
38-
let mut result = String::new();
39-
40-
for _ in 0..rng.gen_range(1..=max_length) {
41-
if rng.gen_bool(0.9) {
42-
let ch = valid_utf8.choose(&mut rng).unwrap();
43-
result.push(*ch);
44-
} else {
45-
let ch = invalid_utf8.choose(&mut rng).unwrap();
46-
if let Some(c) = char::from_u32(*ch as u32) {
47-
result.push(c);
48-
}
49-
}
50-
}
51-
52-
result
53-
}
54-
5534
#[derive(Debug, Clone)]
5635
struct TestArg {
5736
arg: String,

0 commit comments

Comments
 (0)