From ebb8bab06059d4d97695eda24d4748ab94ab367a Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Wed, 11 Nov 2020 11:10:27 -0800 Subject: process: fix potential file descriptor leak (#3129) --- tokio/src/process/unix/mod.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'tokio') diff --git a/tokio/src/process/unix/mod.rs b/tokio/src/process/unix/mod.rs index 6e970ee8..966c2a28 100644 --- a/tokio/src/process/unix/mod.rs +++ b/tokio/src/process/unix/mod.rs @@ -148,6 +148,13 @@ pub(crate) struct Pipe { fd: File, } +impl From for Pipe { + fn from(fd: T) -> Self { + let fd = unsafe { File::from_raw_fd(fd.into_raw_fd()) }; + Self { fd } + } +} + impl<'a> io::Read for &'a Pipe { fn read(&mut self, bytes: &mut [u8]) -> io::Result { (&self.fd).read(bytes) @@ -208,8 +215,9 @@ where }; // Set the fd to nonblocking before we pass it to the event loop - let fd = unsafe { - let fd = io.into_raw_fd(); + let pipe = unsafe { + let pipe = Pipe::from(io); + let fd = pipe.as_raw_fd(); let r = libc::fcntl(fd, libc::F_GETFL); if r == -1 { return Err(io::Error::last_os_error()); @@ -219,8 +227,8 @@ where return Err(io::Error::last_os_error()); } - File::from_raw_fd(fd) + pipe }; - Ok(Some(PollEvented::new(Pipe { fd })?)) + Ok(Some(PollEvented::new(pipe)?)) } -- cgit v1.2.3