Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,9 @@ FS.staticInit();` +
// object isn't directly passed in. not possible until
// SOCKFS is completed.
createStream(stream, fd = -1) {
#if ASSERTIONS
assert(fd >= -1);
#endif

// clone it, so we can return an instance of FSStream
stream = Object.assign(new FS.FSStream(), stream);
Expand Down
2 changes: 2 additions & 0 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,8 @@ var SyscallsLibrary = {
assert(!flags);
#endif
if (old.fd === newfd) return -{{{ cDefs.EINVAL }}};
// Check newfd is within range of valid open file descriptors.
if (newfd < 0 || newfd >= FS.MAX_OPEN_FDS) 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).

if (existing) FS.close(existing);
return FS.dupStream(old, newfd).fd;
Expand Down
14 changes: 13 additions & 1 deletion test/unistd/dup.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,26 @@ int main() {
printf("\n");
errno = 0;

printf("DUP2 err\n");
printf("DUP2 bad fds\n");
f = dup2(-2, -2);
printf("f: %d\n", f == -1);
printf("errno: %d\n", errno);
printf("close(f): %d\n", close(f));
printf("\n");
errno = 0;

printf("DUP2 bad newfd\n");
f = open("/", O_RDONLY);
f3 = dup2(f, -1);
printf("f3: %d\n", f3);
printf("errno: %d\n", errno);
f3 = dup2(f, 256000);
printf("f3: %d\n", f3);
printf("errno: %d\n", errno);
printf("close(f1): %d\n", close(f));
printf("\n");
errno = 0;

printf("DUP2 pipe\n");
int p[2];
pipe(p);
Expand Down
9 changes: 8 additions & 1 deletion test/unistd/dup.out
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ close(f1): 0
close(f2): 0
close(f3): -1

DUP2 err
DUP2 bad fds
f: 1
errno: 8
close(f): -1

DUP2 bad newfd
f3: -1
errno: 8
f3: -1
errno: 8
close(f1): 0

DUP2 pipe
buf: abc

Expand Down