From 6cd5506897d5aadb57455f5081ff8efd77f93b69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Thu, 8 Oct 2020 00:00:00 +0000 Subject: [PATCH] Check for errors returned from posix_spawn*_init functions The posix_spawnattr_init & posix_spawn_file_actions_init might fail, but their return code is not checked. Check for non-zero return code and destroy only succesfully initialized objects. --- .../std/src/sys/unix/process/process_unix.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/library/std/src/sys/unix/process/process_unix.rs b/library/std/src/sys/unix/process/process_unix.rs index 520e5e5d1c5..5e55f97705d 100644 --- a/library/std/src/sys/unix/process/process_unix.rs +++ b/library/std/src/sys/unix/process/process_unix.rs @@ -319,9 +319,9 @@ fn posix_spawn_file_actions_addchdir_np( let mut p = Process { pid: 0, status: None }; - struct PosixSpawnFileActions(MaybeUninit); + struct PosixSpawnFileActions<'a>(&'a mut MaybeUninit); - impl Drop for PosixSpawnFileActions { + impl Drop for PosixSpawnFileActions<'_> { fn drop(&mut self) { unsafe { libc::posix_spawn_file_actions_destroy(self.0.as_mut_ptr()); @@ -329,9 +329,9 @@ fn drop(&mut self) { } } - struct PosixSpawnattr(MaybeUninit); + struct PosixSpawnattr<'a>(&'a mut MaybeUninit); - impl Drop for PosixSpawnattr { + impl Drop for PosixSpawnattr<'_> { fn drop(&mut self) { unsafe { libc::posix_spawnattr_destroy(self.0.as_mut_ptr()); @@ -344,11 +344,13 @@ fn cvt_nz(error: libc::c_int) -> io::Result<()> { } unsafe { - let mut file_actions = PosixSpawnFileActions(MaybeUninit::uninit()); - let mut attrs = PosixSpawnattr(MaybeUninit::uninit()); + let mut attrs = MaybeUninit::uninit(); + cvt_nz(libc::posix_spawnattr_init(attrs.as_mut_ptr()))?; + let attrs = PosixSpawnattr(&mut attrs); - libc::posix_spawnattr_init(attrs.0.as_mut_ptr()); - libc::posix_spawn_file_actions_init(file_actions.0.as_mut_ptr()); + let mut file_actions = MaybeUninit::uninit(); + cvt_nz(libc::posix_spawn_file_actions_init(file_actions.as_mut_ptr()))?; + let file_actions = PosixSpawnFileActions(&mut file_actions); if let Some(fd) = stdio.stdin.fd() { cvt_nz(libc::posix_spawn_file_actions_adddup2(