summaryrefslogtreecommitdiffstats
path: root/tokio
diff options
context:
space:
mode:
authorIvan Petkov <ivanppetkov@gmail.com>2020-11-11 11:10:27 -0800
committerGitHub <noreply@github.com>2020-11-11 11:10:27 -0800
commitebb8bab06059d4d97695eda24d4748ab94ab367a (patch)
tree571e5648146270a7aa951f3ca4887c940fc73ff2 /tokio
parentce891a4df17e632f7557dd0cd1f1e8da89bd6ae4 (diff)
process: fix potential file descriptor leak (#3129)
Diffstat (limited to 'tokio')
-rw-r--r--tokio/src/process/unix/mod.rs16
1 files changed, 12 insertions, 4 deletions
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<T: IntoRawFd> From<T> 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<usize> {
(&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)?))
}