Skip to content

Commit 74f7fb0

Browse files
oech3cakebaker
authored andcommitted
fuzz: remove unsafe
1 parent 2036729 commit 74f7fb0

1 file changed

Lines changed: 4 additions & 12 deletions

File tree

fuzz/uufuzz/src/lib.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ use std::env::temp_dir;
1717
use std::ffi::OsString;
1818
use std::fs::File;
1919
use std::io::{Seek, SeekFrom, Write};
20-
use std::os::fd::{AsRawFd, OwnedFd, RawFd};
21-
use std::os::unix::io::FromRawFd;
2220
use std::process::{Command, Stdio};
2321
use std::sync::atomic::Ordering;
2422
use std::sync::{Once, atomic::AtomicBool};
@@ -158,8 +156,8 @@ where
158156
};
159157

160158
let (uumain_exit_status, captured_stdout, captured_stderr) = thread::scope(|s| {
161-
let out = s.spawn(|| read_from_fd(read_pipe_stdout.as_raw_fd()));
162-
let err = s.spawn(|| read_from_fd(read_pipe_stderr.as_raw_fd()));
159+
let out = s.spawn(|| read_from_fd(read_pipe_stdout));
160+
let err = s.spawn(|| read_from_fd(read_pipe_stderr));
163161
#[allow(clippy::unnecessary_to_owned)]
164162
// TODO: clippy wants us to use args.iter().cloned() ?
165163
let status = uumain_function(args.to_owned().into_iter());
@@ -200,15 +198,12 @@ where
200198
}
201199
}
202200

203-
fn read_from_fd(fd: RawFd) -> String {
201+
fn read_from_fd(fd: impl std::os::fd::AsFd) -> String {
204202
let mut captured_output = Vec::new();
205203
let mut read_buffer = [0; 1024];
206204

207-
// Temporarily create an OwnedFd for reading (we won't drop it)
208-
let owned_fd = unsafe { OwnedFd::from_raw_fd(fd) };
209-
210205
loop {
211-
match read(&owned_fd, &mut read_buffer) {
206+
match read(&fd, &mut read_buffer) {
212207
Ok(bytes_read) => {
213208
if bytes_read == 0 {
214209
break;
@@ -222,9 +217,6 @@ fn read_from_fd(fd: RawFd) -> String {
222217
}
223218
}
224219

225-
// Forget the owned_fd to prevent it from closing the fd (the caller owns it)
226-
std::mem::forget(owned_fd);
227-
228220
String::from_utf8_lossy(&captured_output).into_owned()
229221
}
230222

0 commit comments

Comments
 (0)