[libc-commits] [libc] [libc] fwrite_unlocked: only return errno if an actual error occurred. (PR #167350)
via libc-commits
libc-commits at lists.llvm.org
Mon Nov 10 09:40:58 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: None (Sterling-Augustine)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/167350.diff
1 Files Affected:
- (modified) libc/src/stdio/printf_core/vfprintf_internal.h (+8-2)
``````````diff
diff --git a/libc/src/stdio/printf_core/vfprintf_internal.h b/libc/src/stdio/printf_core/vfprintf_internal.h
index 564441d3bf51a..a14ddee5f62fd 100644
--- a/libc/src/stdio/printf_core/vfprintf_internal.h
+++ b/libc/src/stdio/printf_core/vfprintf_internal.h
@@ -51,8 +51,14 @@ 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.
+ auto bytes = ::fwrite_unlocked(ptr, size, nmemb, f);
+ if (bytes == nmemb * size)
+ return bytes;
+ else
+ return errno;
}
#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
} // namespace internal
``````````
</details>
https://github.com/llvm/llvm-project/pull/167350
More information about the libc-commits
mailing list