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

Add checks to the conversion of Unix file descriptors to I/O channels #1825

Merged
merged 5 commits into from Jun 14, 2018

Conversation

xavierleroy
Copy link
Contributor

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

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) {
Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

return 0;
default: {
DWORD err = GetLastError();
return err == 0 ? ERROR_INVALID_ACCESS : err;
Copy link

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

Copy link
Contributor Author

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

Copy link

@ghost ghost left a 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.

@xavierleroy xavierleroy merged commit ea9dc4e into ocaml:trunk Jun 14, 2018
@xavierleroy xavierleroy deleted the check-stream-semantics branch June 14, 2018 08:56
EmileTrotignon pushed a commit to EmileTrotignon/ocaml that referenced this pull request Jan 12, 2024
* 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>
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.

None yet

2 participants