remove wide printf dependency on ugly hack in vfprintf
authorRich Felker <dalias@aerifal.cx>
Mon, 20 Mar 2023 22:59:14 +0000 (18:59 -0400)
committerRich Felker <dalias@aerifal.cx>
Tue, 21 Mar 2023 13:11:17 +0000 (09:11 -0400)
commit868c96430093cd864512283b43071cb70b39b24e
treeac42e807ad3f6169ae7d4c5ed3fd06482c605c8b
parent3a051769c4a91c3a7d1f1310d888faa4abf363e7
remove wide printf dependency on ugly hack in vfprintf

commit d42269d7c85308abdbf8cee38b1a1097249eb38b appropriated the
stream error flag temporarily to let the printf family of functions
suppress further output attempts after encountering a write error.
since the wide printf code relies on (narrow) vfprintf to print
padding and numeric conversions, a hack was put in vfprintf not to
clear the initial error status unless the stream is narrow oriented.
this was okay, because calling vfprintf on a wide-oriented stream
(outside of internal use by the implementation) produces undefined
behavior. however, it was highly non-obvious to anyone reading the
wide printf code, where the calls to fprintf without first checking
for error status appeared erroneous.

this patch removes all direct use of fprintf from the wide printf
core, except in the numeric conversions case where it was already
checked before starting processing of the directive that the error
status is not set. the other calls, which were performing padding, are
replaced by a new pad() helper function, which performs the check and
abstracts out the mechanism of writing the padding.

direct use of the error flag is also replaced by ferror, which is
defined as a macro in stdio_impl.h, expanding directly to the flag
check with no call or locking overhead.
src/stdio/vfprintf.c
src/stdio/vfwprintf.c