summaryrefslogtreecommitdiff
path: root/libc/stdio/stdio_ext.cpp
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2016-01-19 15:46:05 -0800
committerElliott Hughes <enh@google.com>2016-01-19 15:46:05 -0800
commit923f165b29866cba1bd077117127f576763b384d (patch)
treec5c05829f199caba0e483769023eed1e3d37206c /libc/stdio/stdio_ext.cpp
parent579f42b78922d8c70e19ea77d2818ec17d0dc6a7 (diff)
Make FILE*s less usable after fclose(3).
BSD doesn't invalidate the fd stored in struct FILE, which can make it possible (via fileno(3), for example), to perform operations on an fd you didn't intend to (rather than just failing with EBADF). Fixing this makes the code slightly simpler anyway, and might help catch bad code before it ships. Bug: http://stackoverflow.com/questions/10816837/fclose-works-differently-on-android-and-linux Change-Id: I9db74584038229499197a2695c70b58ed0372a87
Diffstat (limited to 'libc/stdio/stdio_ext.cpp')
-rw-r--r--libc/stdio/stdio_ext.cpp9
1 files changed, 8 insertions, 1 deletions
diff --git a/libc/stdio/stdio_ext.cpp b/libc/stdio/stdio_ext.cpp
index f273d45fe..88e59510d 100644
--- a/libc/stdio/stdio_ext.cpp
+++ b/libc/stdio/stdio_ext.cpp
@@ -27,6 +27,8 @@
*/
#include <stdio_ext.h>
+
+#include <errno.h>
#include <stdlib.h>
#include "local.h"
@@ -101,5 +103,10 @@ int ferror_unlocked(FILE* fp) {
}
int fileno_unlocked(FILE* fp) {
- return __sfileno(fp);
+ int fd = fp->_file;
+ if (fd == -1) {
+ errno = EBADF;
+ return -1;
+ }
+ return fd;
}