Skip to content

Commit 2ed0f4c

Browse files
committed
refactor: I/O safety for add_dup2
Signed-off-by: tison <[email protected]>
1 parent bcbcb50 commit 2ed0f4c

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/spawn.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Safe wrappers around posix_spawn* functions found in the libc "spawn.h" header.
22
3-
use std::{ffi::CStr, mem, os::fd::RawFd};
4-
53
#[cfg(any(feature = "fs", feature = "term"))]
64
use crate::fcntl::OFlag;
75
#[cfg(feature = "signal")]
86
use crate::sys::signal::SigSet;
97
#[cfg(feature = "fs")]
108
use crate::sys::stat::Mode;
119
use crate::{errno::Errno, unistd::Pid, NixPath, Result};
10+
use std::os::fd::AsRawFd;
11+
use std::{ffi::CStr, mem, os::fd::RawFd};
1212

1313
/// A spawn attributes object. See [posix_spawnattr_t](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_init.html).
1414
#[repr(transparent)]
@@ -277,7 +277,14 @@ impl PosixSpawnFileActions {
277277
/// Add a [dup2](https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup2.html) action. See
278278
/// [posix_spawn_file_actions_adddup2](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_adddup2.html).
279279
#[doc(alias("posix_spawn_file_actions_adddup2"))]
280-
pub fn add_dup2(&mut self, fd: RawFd, newfd: RawFd) -> Result<()> {
280+
pub fn add_dup2<Fd, NewFd>(&mut self, fd: Fd, newfd: NewFd) -> Result<()>
281+
where
282+
Fd: std::os::fd::AsFd,
283+
NewFd: std::os::fd::AsFd,
284+
{
285+
let fd = fd.as_fd().as_raw_fd();
286+
let newfd = newfd.as_fd().as_raw_fd();
287+
281288
let res = unsafe {
282289
libc::posix_spawn_file_actions_adddup2(
283290
&mut self.fa as *mut libc::posix_spawn_file_actions_t,

0 commit comments

Comments
 (0)