From c9cb83a5f6a4ec315b187afcf899d5758647dd0f Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 1 Feb 2021 20:00:48 -0700 Subject: Don't implement Clone on Dir, SignalFd, and PtyMaster Since they close their file descriptors on Drop, it's almost impossible to use Clone without creating a double-close situation. Also, check for EBADF in SignalFd::drop and Dir::drop. --- src/dir.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/dir.rs') diff --git a/src/dir.rs b/src/dir.rs index 1d48f18c..1898950f 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -25,7 +25,7 @@ use libc::{dirent, readdir_r}; /// * returns entries for `.` (current directory) and `..` (parent directory). /// * returns entries' names as a `CStr` (no allocation or conversion beyond whatever libc /// does). -#[derive(Clone, Debug, Eq, Hash, PartialEq)] +#[derive(Debug, Eq, Hash, PartialEq)] pub struct Dir( ptr::NonNull ); @@ -85,7 +85,10 @@ impl AsRawFd for Dir { impl Drop for Dir { fn drop(&mut self) { - unsafe { libc::closedir(self.0.as_ptr()) }; + let e = Errno::result(unsafe { libc::closedir(self.0.as_ptr()) }); + if !std::thread::panicking() && e == Err(Error::Sys(Errno::EBADF)) { + panic!("Closing an invalid file descriptor!"); + }; } } -- cgit v1.2.3