Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions libc/src/stdio/printf_core/vfprintf_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 bytes = ::fwrite_unlocked(ptr, size, nmemb, f);
return {bytes, bytes == size ? 0 : errno};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fwrite() function shall return the number of elements successfully written, which may be less than nitems if a write error is encountered.

https://man7.org/linux/man-pages/man3/fwrite.3p.html#RETURN_VALUE

I think that instead of bytes == size ? 0, it should be bytes == nmemb ? 0. Could we also update the name bytes to reflect that it's the number of elements rather than number of bytes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleaned up.

}
#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
} // namespace internal
Expand Down
Loading