Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safe fd wrappers #1184

Closed
wants to merge 4 commits into from
Closed

Safe fd wrappers #1184

wants to merge 4 commits into from

Conversation

Chronophagus
Copy link

Here I'm trying to resolve #594 #678. Wish me luck!

@Chronophagus Chronophagus requested a review from asomers February 8, 2020 11:11
Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you intending to modify all of Nix's functions to take a FileDescriptor?


impl Drop for FileDescriptor {
fn drop(&mut self) {
unistd::close(self.0).expect("You've already closed this file descriptor before me =(");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close can also fail for reasons like EINTR. You shouldn't panic in such cases.

@@ -12,7 +12,7 @@
#![recursion_limit = "500"]
#![deny(unused)]
#![deny(unstable_features)]
#![deny(missing_copy_implementations)]
// #![deny(missing_copy_implementations)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably you're planning to fix this before merging?

let this_fd = self.as_raw_fd();
let other_fd = other.as_raw_fd();

std::mem::forget(other);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a comment explaining why it's ok to use mem::forget?

unsafe { Ok(T::from_raw_fd(ret_fd)) }
}

/// You must guarantee the RawFd isn't owned by any of fd safe wrappers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment belongs in a # Safety section.

std::mem::forget(other);

// TODO: Make this call unsafe
let ret_fd = dup2(this_fd, other_fd)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to call dup2 before calling mem::forget, so that you don't deallocate other if dup2 fails. In that event, you should probably return other to the caller. That would make the error type a std::result::Result<T, (nix::Error, T)>

let this_fd = self.as_raw_fd();
let other_fd = other.as_raw_fd();

std::mem::forget(other);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if a user implements FdOps for his own type, and Drop does something more than just close? In that case, this code would be wrong. Perhaps you should make FdOps a private trait. If not, at least document the strict requirement of drop's behavior.


impl FromRawFd for StdHandle {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
assert!(fd < 3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1 to magic numbers. Instead of hard-coding 3, you should compare to the libc::STDERR_FILENO etc

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Taking RawFd by value is not sound
2 participants