RE: [PATCH] Fix stdio init handling
Torbjorn SVENSSON via Newlib <[email protected]>
| Newsgroups | gmane.comp.lib.newlib |
|---|---|
| Message-ID | <AM6PR10MB2197E7C4551EF2DA1D80A12081A59@AM6PR10MB2197.EURPRD10.PROD.OUTLOOK.COM> |
> -----Original Message----- > From: Newlib <newlib- > [email protected]> On Behalf Of Hans- > Peter Nilsson > Sent: den 6 juni 2022 20:20 > To: [email protected] > 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