Skip to content

Potential double close in PtyMaster #659

Closed
@asomers

Description

@asomers

PtyMaster implements Drop to close its RawFd. However, it ignores errors, because the caller may have manually closed the RawFd. This is bad, because it can lead to double closes, like this:

{
    let m = posix_openpt(O_RDWR);		# Creates a pty with file descriptor 3
    close(m.master);				# Close file descriptor 3
    let f = std::fs::File::create("foo");	# Creates a file with file descriptor 3
}						# PtyMaster::Drop closes file descriptor 3
f.write("whatever");				# fails with EBADF

There are three possible solutions:

  1. Always check for errors in PtyMaster::Drop
  2. Don't implement PtyMaster::Drop; make the caller responsible for closing it. If we go this route, we should also add a PtyMaster::close method.
  3. Change PtyMaster to wrap an Option<RawFd> and have the close method set it to None. That way, the drop method will know whether to call close.

I prefer option 1, but I could go with any of these solutions.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions