diff options
| author | Sterling-Augustine <saugustine@google.com> | 2025-11-10 14:58:26 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-10 14:58:26 -0800 |
| commit | c5ce8021da09cfe232abe2e869269297b00fbcdb (patch) | |
| tree | 89e73ec65c49383d1a4081877ce4a4168311d467 /libc/src/stdio/printf_core/vfprintf_internal.h | |
| parent | 5b8e86909c94ad81b4c2f0cee50c3433ec318788 (diff) | |
[libc] fwrite_unlocked: only return errno if an actual error occurred. (#167350)
fwrite and friends don't modify errno if no error occurred. Therefore
frite_unlocked's return value shouldn't be constructed from errno
without checking if an error actually occurred.
This fixes an error introduced by
https://github.com/llvm/llvm-project/commit/9e2f73fe9052a4fbf382a06e30b2441c6d99fb7e
Diffstat (limited to 'libc/src/stdio/printf_core/vfprintf_internal.h')
| -rw-r--r-- | libc/src/stdio/printf_core/vfprintf_internal.h | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/libc/src/stdio/printf_core/vfprintf_internal.h b/libc/src/stdio/printf_core/vfprintf_internal.h index 564441d3bf51..c47a03d741f9 100644 --- a/libc/src/stdio/printf_core/vfprintf_internal.h +++ b/libc/src/stdio/printf_core/vfprintf_internal.h @@ -51,8 +51,11 @@ LIBC_INLINE void funlockfile(::FILE *f) { ::funlockfile(f); } LIBC_INLINE FileIOResult fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, ::FILE *f) { // Need to use system errno in this case, as system write will set this errno - // which we need to propagate back into our code. - return {::fwrite_unlocked(ptr, size, nmemb, f), errno}; + // which we need to propagate back into our code. fwrite only modifies errno + // if there was an error, and errno may have previously been nonzero. Only + // return errno if there was an error. + size_t members_written = ::fwrite_unlocked(ptr, size, nmemb, f); + return {members_written, members_written == nmemb ? 0 : errno}; } #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE } // namespace internal |
