[PATCH] Fix stdio init handling
Torbjorn SVENSSON
torbjorn.svensson@st.com
Tue Jun 7 06:05:01 GMT 2022
> -----Original Message-----
> From: Newlib <newlib-
> bounces+torbjorn.svensson=st.com@sourceware.org> On Behalf Of Hans-
> Peter Nilsson
> Sent: den 6 juni 2022 20:20
> To: newlib@sourceware.org
> Subject: [PATCH] Fix stdio init handling
>
> Ok to commit?
>
> ----- 8< -----
> After commit e826fbb2ae88 "Fix stdio exit handling", for trivial
> newlib using targets such as cris-elf and arm-eabihf, i.e.
> (!_REENT_SMALL && !_REENT_GLOBAL_STDIO_STREAMS && !cygwin) stdio
> initialization no longer happens properly. At that commit and
> after, programs like the following are broken for such targets;
> at the first opened file, the first FILE record seems
> pre-allocated, unused and free, and is returned as the "FILE *"
> at the fopen. At the subsequent fgetc, it's *initialized* as
> stdin and the fgetc returns EOF (without errno), yielding
> "fgetc-EOF: Success" and the program aborted instead of the
> expected "all-ok".
>
> ===============
> /* There must exist a file "./fff" with the first byte an 'f'. */
> #include <stdio.h>
> #include <errno.h>
> #include <stdlib.h>
> int main(void)
> {
> int e = (errno = 0);
> FILE *f = fopen("fff", "r");
> int c = fgetc(f);
>
> if (f == NULL)
> {
> perror("fopen");
> abort();
> }
>
Unrelated to the patch proposed, but I think you should avoid calling fgetc(f) prior to checking if f is NULL or a valid pointer.
Kind regards,
Torbjörn
More information about the Newlib
mailing list