summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdio/fopen.c
diff options
context:
space:
mode:
authorotto <otto@openbsd.org>2008-04-21 12:28:35 +0000
committerotto <otto@openbsd.org>2008-04-21 12:28:35 +0000
commit67011cebfe037fa5d895ae9ae974133bc80d0783 (patch)
tree0652aaef628b760068754b61b79042f06c2dd619 /lib/libc/stdio/fopen.c
parentInitialize the correct variable in HUP handler if ed.hup in current (diff)
downloadwireguard-openbsd-67011cebfe037fa5d895ae9ae974133bc80d0783.tar.xz
wireguard-openbsd-67011cebfe037fa5d895ae9ae974133bc80d0783.zip
_file is only a short, so prevent truncation if we happen to hit
upon a fd > SHRT_MAX. From freebsd via Jan Schaumann; ok deraadt@ millert@ espie@
Diffstat (limited to 'lib/libc/stdio/fopen.c')
-rw-r--r--lib/libc/stdio/fopen.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/lib/libc/stdio/fopen.c b/lib/libc/stdio/fopen.c
index b063e4545f8..64f81ea77ab 100644
--- a/lib/libc/stdio/fopen.c
+++ b/lib/libc/stdio/fopen.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fopen.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */
+/* $OpenBSD: fopen.c,v 1.6 2008/04/21 12:28:35 otto Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -34,6 +34,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
+#include <limits.h>
#include <stdio.h>
#include <errno.h>
#include "local.h"
@@ -53,6 +54,15 @@ fopen(const char *file, const char *mode)
fp->_flags = 0; /* release */
return (NULL);
}
+
+ /* _file is only a short */
+ if (f > SHRT_MAX) {
+ fp->_flags = 0; /* release */
+ close(f);
+ errno = EMFILE;
+ return (NULL);
+ }
+
fp->_file = f;
fp->_flags = flags;
fp->_cookie = fp;