public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Torbjorn SVENSSON <torbjorn.svensson@st.com>
To: Hans-Peter Nilsson <hp@axis.com>,
	"newlib@sourceware.org" <newlib@sourceware.org>
Subject: RE: [PATCH] Fix stdio init handling
Date: Tue, 7 Jun 2022 06:05:01 +0000	[thread overview]
Message-ID: <AM6PR10MB2197E7C4551EF2DA1D80A12081A59@AM6PR10MB2197.EURPRD10.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <20220606182003.DB8A72041A@pchp3.se.axis.com>



> -----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

  parent reply	other threads:[~2022-06-07  6:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-06 18:20 Hans-Peter Nilsson
2022-06-06 20:16 ` Jeff Johnston
2022-06-07  6:21   ` Sebastian Huber
2022-06-07 14:47     ` Jeff Johnston
2022-06-07  6:05 ` Torbjorn SVENSSON [this message]
2022-06-07 14:40   ` Hans-Peter Nilsson
2022-06-07  6:18 ` Sebastian Huber
2022-06-07 19:31   ` Sebastian Huber
2022-06-07 23:30   ` Hans-Peter Nilsson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AM6PR10MB2197E7C4551EF2DA1D80A12081A59@AM6PR10MB2197.EURPRD10.PROD.OUTLOOK.COM \
    --to=torbjorn.svensson@st.com \
    --cc=hp@axis.com \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).