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
Add checks to the conversion of Unix file descriptors to I/O channels #1825
Conversation
Have these functions fail if the given file descriptor is not suitable for character-oriented I/O, e.g. a block device or a datagram socket. Part of MPR#7238. This is WIP. Needs porting to Windows and testing.
Added Win32 implementation. Changed Unix implementation to report EINVAL in case of non-stream semantics.
struct stat buf; | ||
|
||
if (fstat(fd, &buf) == -1) return errno; | ||
switch (buf.st_mode & S_IFMT) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is sufficient, but someone who can read POSIXese will have to say if one of these is set when the various S_TYPE
macros for memory queues and semaphores and the like are true. It may be that one has to check for that separately?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, thinking about this, if none of those bits are set, the default:
case will trigger anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I know, memory queues and semaphores cannot be opened as file descriptors, e.g. sem_open
returns a sem_t *
, and is not made available by the OCaml Unix library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So my comment was inapplicable two distinct ways.
otherlibs/win32unix/channels.c
Outdated
return 0; | ||
default: { | ||
DWORD err = GetLastError(); | ||
return err == 0 ? ERROR_INVALID_ACCESS : err; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would write NO_ERROR
here, which is slightly clearer than 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice suggestion, implemented in 0724940
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I read the code and it looks correct to me. This check seems useful to me.
Use `NO_ERROR` instead of `0` to mean "no error occurs".
Added Changes entry.
* Create tutorial: Running Commands in an Opam Switch --------- Co-authored-by: Cuihtlauac ALVARADO <cuihtmlauac@tarides.com> Co-authored-by: sabine <sabine@users.noreply.github.com> Co-authored-by: Christine Rose <christinerose@users.noreply.github.com>
This GPR adds checks to the
Unix.{in,out}_channel_of_descr
functions so that they fail if the given file descriptor is not suitable for character-oriented I/O, e.g. a block device or a datagram socket.Without such checks, the I/O channel is created, but will most likely fail in mysterious ways when flushed or refilled.
This addresses part of MPR#7238