From 923f165b29866cba1bd077117127f576763b384d Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 19 Jan 2016 15:46:05 -0800 Subject: 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 --- libc/stdio/stdio_ext.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'libc/stdio/stdio_ext.cpp') 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 + +#include #include #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; } -- cgit v1.2.3