[PATCH v2] newlib: libc: Fix bugs in the commit 3d94e07c49b5.

Brian Inglis Brian.Inglis@Shaw.ca
Wed Nov 15 22:53:18 GMT 2023


On 2023-11-15 09:31, Corinna Vinschen wrote:
> Also, as in GLibC, do not set orientation in ungetc, and only set, but
> do not check orientation in ungetwc.

Function ungetwc is a wide character input/output function, as ungetc is a byte 
input/output function, as per N3096 C202X CD2 2023-04 7.23 Input/output 
<stdio.h> 7.23.1 Introduction #6:

"6 The input/output functions are given the following collective terms:
— The wide character input functions — those functions described in 7.31 that 
perform input into wide characters and wide strings: fgetwc, fgetws, getwc, 
getwchar, fwscanf, wscanf, vfwscanf, and vwscanf.
— The wide character output functions — those functions described in 7.31 that 
perform output from wide characters and wide strings: fputwc, fputws, putwc, 
putwchar, fwprintf, wprintf, vfwprintf, and vwprintf.
— The wide character input/output functions — the union of the ungetwc function, 
the wide character input functions, and the wide character output functions.
— The byte input/output functions — those functions described in this subclause 
that perform input/output: fgetc, fgets, fprintf, fputc, fputs, fread, fscanf, 
fwrite, getc, getchar, printf, putc, putchar, puts, scanf, ungetc, vfprintf, 
vfscanf, vprintf, and vscanf."

and POSIX 202X Draft 3 concurs in 3.417 and 3.56.

All functions of incorrect orientation are invalid once orientation is set, so 
in ungetwc and ungetc, orientation should be checked, as in any of the input and 
output functions named, and return W/EOF if the the stream is the opposite 
orientation, as normal operation is not successful, as per the function 
definitions, and C202X CD2 7.23.2 Streams #4-5:

"4 Each stream has an orientation. After a stream is associated with an external 
file, but before any operations are performed on it, the stream is unoriented.
Once a wide character input/output function has been applied to an unoriented 
stream, the stream becomes a wide-oriented stream.
Similarly, once a byte input/output function has been applied to an unoriented 
stream, the stream becomes a byte-oriented stream.
Only a call to the freopen function or the fwide function can otherwise alter 
the orientation of a stream. (A successful call to freopen removes any orientation.)
5 Byte input/output functions shall not be applied to a wide-oriented stream and 
wide character input/output functions shall not be applied to a byte-oriented 
stream."

and POSIX 202X D3 concurs in 2.5.2 Stream Orientation and Encoding Rules.

In C202X CD2 Annex J Portability issues J.2 Undefined behavior specifies:

"(149) A byte input/output function is applied to a wide-oriented stream, or a 
wide character input/output function is applied to a byte-oriented stream (7.23.2)."

-- 
Take care. Thanks, Brian Inglis              Calgary, Alberta, Canada

La perfection est atteinte                   Perfection is achieved
non pas lorsqu'il n'y a plus rien à ajouter  not when there is no more to add
mais lorsqu'il n'y a plus rien à retirer     but when there is no more to cut
                                 -- Antoine de Saint-Exupéry


More information about the Newlib mailing list