fix wide printf continuation after output or encoding errors
authorRich Felker <dalias@aerifal.cx>
Mon, 20 Mar 2023 22:48:20 +0000 (18:48 -0400)
committerRich Felker <dalias@aerifal.cx>
Tue, 21 Mar 2023 13:10:11 +0000 (09:10 -0400)
commit0440ed69eac766c712e974358c3e09d63e330f40
tree89c12b1b0467a4e4ec6b7520c94b646eb837adf7
parentd055e6a45a17673b8dd3ec16e786bb2fe1700dd5
fix wide printf continuation after output or encoding errors

this fixes a broader bug for which a special case was reported by
Bruno Haible, in the form of %n getting processed (and reporting the
number of wide characters which would have been written, but weren't)
after an encoding error (EILSEQ). in addition to the %n case, some but
not all of the format specifiers continued to attempt output after an
error. in particular, %c, %lc, and %s all used fputwc directly without
any check for error status.

as long as the error condition was permanent rather than transient,
these write attempts had no visible side effects, but in theory it
could be visible, for example with EAGAIN/EWOULDBLOCK or ENOSPC, if
the condition precluding output came to an end. this could produce
output with missing non-final data, rather than just truncated output,
albeit with the function still returning -1 as expected to report an
error.

to fix this, a check is added to stop processing of any new directive
(including %n) if the stream is already in error state, and direct use
of fputwc is replaced with calls to the out() helper function, which
checks for error status.

note that fprintf is also used directly without checking error status,
but due to how commit d42269d7c85308abdbf8cee38b1a1097249eb38b
previously attempted to solve the issue of output after error, the
call to fprintf does not attempt to write anything when the
wide-oriented stream is already in error state. this is non-obvious,
and is quite a hack, so it should be changed, but I've left it alone
for now to make the bug fix commit itself as non-invasive as possible.
src/stdio/vfwprintf.c