Skip to content

Commit cb6d339

Browse files
authored
refactor(head): replace unsafe raw fd usage with safe AsFd API (#10161)
Use std::os::fd::AsFd trait and try_clone_to_owned to safely handle stdin file descriptor, eliminating unsafe code for better reliability and adherence to modern Rust standards.
1 parent 3441de9 commit cb6d339

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

src/uu/head/src/head.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::fs::File;
1212
use std::io::{self, BufWriter, Read, Seek, SeekFrom, Write};
1313
use std::num::TryFromIntError;
1414
#[cfg(unix)]
15-
use std::os::fd::{AsRawFd, FromRawFd};
15+
use std::os::fd::AsFd;
1616
use std::path::PathBuf;
1717
use thiserror::Error;
1818
use uucore::display::{Quotable, print_verbatim};
@@ -479,8 +479,8 @@ fn uu_head(options: &HeadOptions) -> UResult<()> {
479479

480480
#[cfg(unix)]
481481
{
482-
let stdin_raw_fd = stdin.as_raw_fd();
483-
let mut stdin_file = unsafe { File::from_raw_fd(stdin_raw_fd) };
482+
let stdin_owned_fd = stdin.as_fd().try_clone_to_owned()?;
483+
let mut stdin_file = File::from(stdin_owned_fd);
484484
let current_pos = stdin_file.stream_position();
485485
if let Ok(current_pos) = current_pos {
486486
// We have a seekable file. Ensure we set the input stream to the

0 commit comments

Comments
 (0)