process: fix potential file descriptor leak (#3129)

This commit is contained in:
Ivan Petkov 2020-11-11 11:10:27 -08:00 committed by GitHub
parent ce891a4df1
commit ebb8bab060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)?))
}