From 1c44c55be02338a09e5eb77931c12e5d4395cdc4 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Sat, 2 Mar 2024 17:54:05 +1100 Subject: [PATCH] Use `BorrowedFd` instead of `ManuallyDrop` for cloning fd Signed-off-by: Jiahao XU --- src/unix.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/unix.rs b/src/unix.rs index b18bf6a..25efef1 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -446,10 +446,10 @@ unsafe fn fd_check(fd: c_int, check_pipe: bool) -> Result<(), FromEnvErrorInner> } fn clone_fd_and_set_cloexec(fd: c_int) -> Result { - // Safety: File is wrapped in `ManuallyDrop` to prevent closing on drop - // since we don't own the fd. - mem::ManuallyDrop::new(unsafe { File::from_raw_fd(fd) }) - .try_clone() + // Safety: fd is a valid fd dand it remains open until returns + unsafe { BorrowedFd::borrow_raw(fd) } + .try_clone_to_owned() + .map(File::from) .map_err(|err| FromEnvErrorInner::CannotOpenFd(fd, err)) }