From 0d67e585236aa74495fe9a914686624ae1da024d Mon Sep 17 00:00:00 2001 From: oech3 <79379754+oech3@users.noreply.github.com> Date: Mon, 27 Apr 2026 16:23:04 +0900 Subject: [PATCH] cat: remove unsafe from test code --- src/uu/cat/Cargo.toml | 3 +++ tests/by-util/test_cat.rs | 28 ++++++++++------------------ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/uu/cat/Cargo.toml b/src/uu/cat/Cargo.toml index 16d3f05be..5814ce2d5 100644 --- a/src/uu/cat/Cargo.toml +++ b/src/uu/cat/Cargo.toml @@ -37,6 +37,9 @@ divan = { workspace = true } tempfile = { workspace = true } uucore = { workspace = true, features = ["benchmark"] } +[target.'cfg(unix)'.dev-dependencies] +rustix = { workspace = true } + [[bin]] name = "cat" path = "src/main.rs" diff --git a/tests/by-util/test_cat.rs b/tests/by-util/test_cat.rs index 3bb623eb1..1ccec792d 100644 --- a/tests/by-util/test_cat.rs +++ b/tests/by-util/test_cat.rs @@ -22,26 +22,18 @@ use uutests::util_name; // Verify cat handles a broken pipe on stdout without hanging or crashing and exits nonzero #[test] fn test_cat_broken_pipe_nonzero_and_message() { - use std::fs::File; - use std::os::unix::io::FromRawFd; use uutests::new_ucmd; + let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe"); + // Close the read end to simulate a broken pipe on stdout + drop(read); + let write: File = write.into(); + let content = (0..10000).map(|_| "x").collect::(); - unsafe { - let mut fds: [libc::c_int; 2] = [0, 0]; - assert_eq!(libc::pipe(fds.as_mut_ptr()), 0, "Failed to create pipe"); - // Close the read end to simulate a broken pipe on stdout - let read_end = File::from_raw_fd(fds[0]); - // Explicitly drop the read-end so writers see EPIPE instead of blocking on a full pipe - drop(read_end); - let write_end = File::from_raw_fd(fds[1]); - - let content = (0..10000).map(|_| "x").collect::(); - // On Unix, SIGPIPE should lead to a non-zero exit; ensure process exits and fails - new_ucmd!() - .set_stdout(write_end) - .pipe_in(content.as_bytes()) - .fails(); - } + // On Unix, SIGPIPE should lead to a non-zero exit; ensure process exits and fails + new_ucmd!() + .set_stdout(write) + .pipe_in(content.as_bytes()) + .fails(); } #[test]