mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
fuzz: move the common duplicated code into a function
This commit is contained in:
parent
f31c017275
commit
f0f64bd3b7
3 changed files with 59 additions and 98 deletions
|
@ -12,9 +12,9 @@ use rand::seq::SliceRandom;
|
|||
use rand::Rng;
|
||||
use std::ffi::OsString;
|
||||
|
||||
use libc::{dup, dup2, STDOUT_FILENO};
|
||||
use std::process::Command;
|
||||
mod fuzz_common;
|
||||
use crate::fuzz_common::generate_and_run_uumain;
|
||||
use crate::fuzz_common::is_gnu_cmd;
|
||||
|
||||
static CMD_PATH: &str = "expr";
|
||||
|
@ -108,52 +108,7 @@ fuzz_target!(|_data: &[u8]| {
|
|||
let mut args = vec![OsString::from("expr")];
|
||||
args.extend(expr.split_whitespace().map(OsString::from));
|
||||
|
||||
// Save the original stdout file descriptor
|
||||
let original_stdout_fd = unsafe { dup(STDOUT_FILENO) };
|
||||
|
||||
// Create a pipe to capture stdout
|
||||
let mut pipe_fds = [-1; 2];
|
||||
unsafe { libc::pipe(pipe_fds.as_mut_ptr()) };
|
||||
let uumain_exit_code;
|
||||
{
|
||||
// Redirect stdout to the write end of the pipe
|
||||
unsafe { dup2(pipe_fds[1], STDOUT_FILENO) };
|
||||
|
||||
// Run uumain with the provided arguments
|
||||
uumain_exit_code = uumain(args.clone().into_iter());
|
||||
|
||||
// Restore original stdout
|
||||
unsafe { dup2(original_stdout_fd, STDOUT_FILENO) };
|
||||
unsafe { libc::close(original_stdout_fd) };
|
||||
}
|
||||
// Close the write end of the pipe
|
||||
unsafe { libc::close(pipe_fds[1]) };
|
||||
|
||||
// Read captured output from the read end of the pipe
|
||||
let mut captured_output = Vec::new();
|
||||
let mut read_buffer = [0; 1024];
|
||||
loop {
|
||||
let bytes_read = unsafe {
|
||||
libc::read(
|
||||
pipe_fds[0],
|
||||
read_buffer.as_mut_ptr() as *mut libc::c_void,
|
||||
read_buffer.len(),
|
||||
)
|
||||
};
|
||||
if bytes_read <= 0 {
|
||||
break;
|
||||
}
|
||||
captured_output.extend_from_slice(&read_buffer[..bytes_read as usize]);
|
||||
}
|
||||
|
||||
// Close the read end of the pipe
|
||||
unsafe { libc::close(pipe_fds[0]) };
|
||||
|
||||
// Convert captured output to a string
|
||||
let rust_output = String::from_utf8_lossy(&captured_output)
|
||||
.to_string()
|
||||
.trim()
|
||||
.to_owned();
|
||||
let (rust_output, uumain_exit_code) = generate_and_run_uumain(&mut args, uumain);
|
||||
|
||||
// Run GNU expr with the provided arguments and compare the output
|
||||
match run_gnu_expr(&args[1..]) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue