summaryrefslogtreecommitdiff
path: root/libc/stdio/stdio.cpp
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2021-02-08 16:24:46 -0800
committerElliott Hughes <enh@google.com>2021-02-09 17:13:09 -0800
commitd3915c7b53b745c521046b0af2455b4db3251c54 (patch)
treee7696b302bdabb3896d808101376cb5337a1fc88 /libc/stdio/stdio.cpp
parent6d5662d22a38ca6200d6b6dd919204bc93dbaba8 (diff)
Make fd overflow an abort.
On LP32, just abort if we're asked to handle an fd that's too big for the `short` field in `struct FILE`. This is unreachable anyway because the ulimit is 32Ki, and this will make issues far more noticeable if we ever do increase that limit (which seems unlikely for LP32 devices). Also rename __finit() to __FILE_init() to match __FILE_close(). Test: treehugger Change-Id: I5db4d6c4529a1f558aff135b4dea071d73666be5
Diffstat (limited to 'libc/stdio/stdio.cpp')
-rw-r--r--libc/stdio/stdio.cpp23
1 files changed, 9 insertions, 14 deletions
diff --git a/libc/stdio/stdio.cpp b/libc/stdio/stdio.cpp
index 2b0e2b21f..c429ff2c3 100644
--- a/libc/stdio/stdio.cpp
+++ b/libc/stdio/stdio.cpp
@@ -226,9 +226,13 @@ extern "C" __LIBC_HIDDEN__ void __libc_stdio_cleanup(void) {
_fwalk(__sflush);
}
-static FILE* __finit(FILE* fp, int fd, int flags) {
+static FILE* __FILE_init(FILE* fp, int fd, int flags) {
if (fp == nullptr) return nullptr;
+#if !defined(__LP64__)
+ if (fd > SHRT_MAX) __fortify_fatal("stdio: fd %d > SHRT_MAX", fd);
+#endif
+
fp->_file = fd;
android_fdsan_exchange_owner_tag(fd, 0, __get_file_tag(fp));
fp->_flags = flags;
@@ -237,15 +241,6 @@ static FILE* __finit(FILE* fp, int fd, int flags) {
fp->_write = __swrite;
fp->_close = __sclose;
_EXT(fp)->_seek64 = __sseek64;
-
-#if !defined(__LP64__)
- if (fd > SHRT_MAX) {
- errno = EMFILE;
- fclose(fp);
- return nullptr;
- }
-#endif
-
return fp;
}
@@ -259,7 +254,7 @@ FILE* fopen(const char* file, const char* mode) {
return nullptr;
}
- FILE* fp = __finit(__sfp(), fd, flags);
+ FILE* fp = __FILE_init(__sfp(), fd, flags);
if (fp == nullptr) {
ErrnoRestorer errno_restorer;
close(fd);
@@ -298,7 +293,7 @@ FILE* fdopen(int fd, const char* mode) {
fcntl(fd, F_SETFD, tmp | FD_CLOEXEC);
}
- return __finit(__sfp(), fd, flags);
+ return __FILE_init(__sfp(), fd, flags);
}
FILE* freopen(const char* file, const char* mode, FILE* fp) {
@@ -398,11 +393,11 @@ FILE* freopen(const char* file, const char* mode, FILE* fp) {
}
}
- fp = __finit(fp, fd, flags);
+ __FILE_init(fp, fd, flags);
// For append mode, O_APPEND sets the write position for free, but we need to
// set the read position manually.
- if (fp && (mode_flags & O_APPEND) != 0) __sseek64(fp, 0, SEEK_END);
+ if ((mode_flags & O_APPEND) != 0) __sseek64(fp, 0, SEEK_END);
return fp;
}