Skip to content

Conversation

@sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Jun 3, 2024

Fixes: #22040

@sbc100 sbc100 requested a review from kripken June 3, 2024 17:38
@sbc100 sbc100 force-pushed the fix_dup2 branch 2 times, most recently from 2d88f24 to f354dab Compare June 3, 2024 17:40
if (old.fd === newfd) return -{{{ cDefs.EINVAL }}};
// Check newfd is within range of valid open file descriptors.
if (newfd < 0 || newfd > 1024) return -{{{ cDefs.EBADF }}};
var existing = FS.getStream(newfd);
Copy link
Member

Choose a reason for hiding this comment

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

Actually, shouldn't this line error if newfd is invalid? That would be a more precise check than the maximum id of an fd.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FS.getStream() returns undefined is the stream is not currently open its not supposed to throw an error. For that we have FS.getStreamChecked() but we don't want to use that here since we want to allows for the case where newfs is not open.

I think this is a reasonable place to do that check since this is the only place where the user can supply "proposed" file descriptor that is not already open (AFAIK).

@sbc100 sbc100 merged commit 3de1b90 into emscripten-core:main Jun 3, 2024
@sbc100 sbc100 deleted the fix_dup2 branch June 3, 2024 20:11
msorvig pushed a commit to msorvig/emscripten that referenced this pull request Jun 4, 2024
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.

::dup2(fd, -1) does not return -1 or set errno

2 participants