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
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index 58109a9..d054bf5 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -1054,3 +1054,17 @@
   fclose(fr);
   fclose(fw);
 }
+
+TEST(STDIO_TEST, fclose_invalidates_fd) {
+  // The typical error we're trying to help people catch involves accessing
+  // memory after it's been freed. But we know that stdin/stdout/stderr are
+  // special and don't get deallocated, so this test uses stdin.
+  ASSERT_EQ(0, fclose(stdin));
+
+  // Even though using a FILE* after close is undefined behavior, I've closed
+  // this bug as "WAI" too many times. We shouldn't hand out stale fds,
+  // especially because they might actually correspond to a real stream.
+  errno = 0;
+  ASSERT_EQ(-1, fileno(stdin));
+  ASSERT_EQ(EBADF, errno);
+}