Skip to content

Commit cfb2d72

Browse files
committedMar 16, 2021
Make do_fork unsafe
1 parent a266bd8 commit cfb2d72

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed
 

‎library/std/src/sys/unix/process/process_unix.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl Command {
5252
// in its own process. Thus the parent drops the lock guard while the child
5353
// forgets it to avoid unlocking it on a new thread, which would be invalid.
5454
let env_lock = sys::os::env_read_lock();
55-
let (result, pidfd) = self.do_fork()?;
55+
let (result, pidfd) = unsafe { self.do_fork()? };
5656

5757
let pid = unsafe {
5858
match result {
@@ -121,14 +121,14 @@ impl Command {
121121
// Attempts to fork the process. If successful, returns Ok((0, -1))
122122
// in the child, and Ok((child_pid, -1)) in the parent.
123123
#[cfg(not(target_os = "linux"))]
124-
fn do_fork(&mut self) -> Result<(pid_t, pid_t), io::Error> {
125-
cvt(unsafe { libc::fork() }).map(|res| (res, -1))
124+
unsafe fn do_fork(&mut self) -> Result<(pid_t, pid_t), io::Error> {
125+
cvt(libc::fork()).map(|res| (res, -1))
126126
}
127127

128128
// Attempts to fork the process. If successful, returns Ok((0, -1))
129129
// in the child, and Ok((child_pid, child_pidfd)) in the parent.
130130
#[cfg(target_os = "linux")]
131-
fn do_fork(&mut self) -> Result<(pid_t, pid_t), io::Error> {
131+
unsafe fn do_fork(&mut self) -> Result<(pid_t, pid_t), io::Error> {
132132
use crate::sync::atomic::{AtomicBool, Ordering};
133133

134134
static HAS_CLONE3: AtomicBool = AtomicBool::new(true);
@@ -183,7 +183,7 @@ impl Command {
183183
let args_ptr = &mut args as *mut clone_args;
184184
let args_size = crate::mem::size_of::<clone_args>();
185185

186-
let res = cvt(unsafe { clone3(args_ptr, args_size) });
186+
let res = cvt(clone3(args_ptr, args_size));
187187
match res {
188188
Ok(n) => return Ok((n as pid_t, pidfd)),
189189
Err(e) => match e.raw_os_error() {
@@ -201,7 +201,7 @@ impl Command {
201201

202202
// If we get here, the 'clone3' syscall does not exist
203203
// or we do not have permission to call it
204-
cvt(unsafe { libc::fork() }).map(|res| (res, pidfd))
204+
cvt(libc::fork()).map(|res| (res, pidfd))
205205
}
206206

207207
pub fn exec(&mut self, default: Stdio) -> io::Error {

0 commit comments

Comments
 (0)