Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect posix_spawn* error checking #2513

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 58 additions & 19 deletions src/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ impl PosixSpawnAttr {
let mut attr = mem::MaybeUninit::uninit();
let res = unsafe { libc::posix_spawnattr_init(attr.as_mut_ptr()) };

Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

let attr = unsafe { attr.assume_init() };
Ok(PosixSpawnAttr { attr })
Expand All @@ -43,14 +45,18 @@ impl PosixSpawnAttr {
&mut self.attr as *mut libc::posix_spawnattr_t,
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

let res = unsafe {
libc::posix_spawnattr_init(
&mut self.attr as *mut libc::posix_spawnattr_t,
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

Ok(self)
}
Expand All @@ -65,7 +71,9 @@ impl PosixSpawnAttr {
flags.bits() as libc::c_short,
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

Ok(())
}
Expand All @@ -81,7 +89,9 @@ impl PosixSpawnAttr {
&mut flags,
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

Ok(PosixSpawnFlags::from_bits_truncate(flags.into()))
}
Expand All @@ -96,7 +106,9 @@ impl PosixSpawnAttr {
pgroup.as_raw(),
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

Ok(())
}
Expand All @@ -113,7 +125,9 @@ impl PosixSpawnAttr {
&mut pid,
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

Ok(Pid::from_raw(pid))
}
Expand All @@ -130,7 +144,9 @@ impl PosixSpawnAttr {
sigdefault.as_ref(),
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

Ok(())
}
Expand All @@ -147,7 +163,9 @@ impl PosixSpawnAttr {
sigset.as_mut_ptr(),
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

let sigdefault =
unsafe { SigSet::from_sigset_t_unchecked(sigset.assume_init()) };
Expand All @@ -164,7 +182,9 @@ impl PosixSpawnAttr {
sigdefault.as_ref(),
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

Ok(())
}
Expand All @@ -181,7 +201,9 @@ impl PosixSpawnAttr {
sigset.as_mut_ptr(),
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

let sigdefault =
unsafe { SigSet::from_sigset_t_unchecked(sigset.assume_init()) };
Expand Down Expand Up @@ -242,7 +264,10 @@ impl PosixSpawnFileActions {
let res = unsafe {
libc::posix_spawn_file_actions_init(actions.as_mut_ptr())
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

Ok(unsafe {
PosixSpawnFileActions {
fa: actions.assume_init(),
Expand All @@ -262,14 +287,18 @@ impl PosixSpawnFileActions {
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

let res = unsafe {
libc::posix_spawn_file_actions_init(
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

Ok(self)
}
Expand All @@ -285,7 +314,9 @@ impl PosixSpawnFileActions {
newfd,
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

Ok(())
}
Expand All @@ -311,7 +342,9 @@ impl PosixSpawnFileActions {
mode.bits(),
)
})?;
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

Ok(())
}
Expand All @@ -327,7 +360,9 @@ impl PosixSpawnFileActions {
fd,
)
};
Errno::result(res)?;
if res != 0 {
return Err(Errno::from_raw(res));
}

Ok(())
}
Expand Down Expand Up @@ -383,8 +418,10 @@ pub fn posix_spawn<SA: AsRef<CStr>, SE: AsRef<CStr>>(
env_p.as_ptr(),
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}

Errno::result(res)?;
Ok(Pid::from_raw(pid))
}

Expand Down Expand Up @@ -412,7 +449,9 @@ pub fn posix_spawnp<SA: AsRef<CStr>, SE: AsRef<CStr>>(
env_p.as_ptr(),
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}

Errno::result(res)?;
Ok(Pid::from_raw(pid))
}
17 changes: 17 additions & 0 deletions test/test_spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus};

#[test]
fn spawn_true() {
let _m = crate::FORK_MTX.lock();
let bin = &CString::new("true").unwrap();
let args = &[
CString::new("true").unwrap(),
Expand All @@ -32,6 +33,8 @@ fn spawn_true() {

#[test]
fn spawn_sleep() {
let _m = crate::FORK_MTX.lock();

let bin = &CString::new("sleep").unwrap();
let args = &[CString::new("sleep").unwrap(), CString::new("30").unwrap()];
let vars: &[CString] = &[];
Expand Down Expand Up @@ -63,3 +66,17 @@ fn spawn_sleep() {
}
};
}

#[test]
fn spawn_fail() {
let _m = crate::FORK_MTX.lock();

let bin = &CString::new("3f0ffc950ccd2fb8").unwrap();
let args = &[CString::new("3f0ffc950ccd2fb8").unwrap()];
let vars: &[CString] = &[];
let actions = PosixSpawnFileActions::init().unwrap();
let attr = PosixSpawnAttr::init().unwrap();

let result = spawn::posix_spawnp(bin, &actions, &attr, args, vars);
assert!(result.is_err());
}
Loading