Skip to content

Commit

Permalink
Use BorrowedFd instead of ManuallyDrop<File> for cloning fd
Browse files Browse the repository at this point in the history
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
  • Loading branch information
NobodyXu committed Mar 2, 2024
1 parent 108d8a4 commit 1c44c55
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<File, FromEnvErrorInner> {
// 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))
}

Expand Down

0 comments on commit 1c44c55

Please sign in to comment.