[libc-commits] [libc] [libc] fwrite_unlocked: only return errno if an actual error occurred. (PR #167350)
Jackson Stogel via libc-commits
libc-commits at lists.llvm.org
Mon Nov 10 10:57:34 PST 2025
================
@@ -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.
+ auto bytes = ::fwrite_unlocked(ptr, size, nmemb, f);
+ return {bytes, bytes == nmemb * size ? 0 : errno};
----------------
jtstogel wrote:
Docs for `fwrite` say say that it returns the number of elements written, not bytes (https://man7.org/linux/man-pages/man3/fwrite.3p.html#RETURN_VALUE). Is fwrite_unlocked different?
https://github.com/llvm/llvm-project/pull/167350
More information about the libc-commits
mailing list