diff options
author | Elliott Hughes <enh@google.com> | 2021-02-10 16:28:34 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2021-02-10 16:28:34 +0000 |
commit | 8460308d11ba6129bea6000cc4fc1a58795c82db (patch) | |
tree | 4e1242b598a8ecd1d714e8124e1e01e9c536a4f5 | |
parent | 8d1f9c2c3b57f725a78addcff05d9abeca38c46d (diff) | |
parent | d3915c7b53b745c521046b0af2455b4db3251c54 (diff) | |
download | bionic-8460308d11ba6129bea6000cc4fc1a58795c82db.tar.gz |
Merge "Make fd overflow an abort."
-rw-r--r-- | libc/stdio/stdio.cpp | 23 |
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; } |