diff options
Diffstat (limited to 'lib/libc/stdio/fdopen.c')
-rw-r--r-- | lib/libc/stdio/fdopen.c | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/libc/stdio/fdopen.c b/lib/libc/stdio/fdopen.c index c199871..02378ed 100644 --- a/lib/libc/stdio/fdopen.c +++ b/lib/libc/stdio/fdopen.c @@ -57,6 +57,18 @@ fdopen(fd, mode) if (nofile == 0) nofile = getdtablesize(); + /* + * File descriptors are a full int, but _file is only a short. + * If we get a valid file descriptor that is greater than + * SHRT_MAX, then the fd will get sign-extended into an + * invalid file descriptor. Handle this case by failing the + * open. + */ + if (fd > SHRT_MAX) { + errno = EMFILE; + return (NULL); + } + if ((flags = __sflags(mode, &oflags)) == 0) return (NULL); |