mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Stefan Ciotec <Stefan.Ciotec@enea.com>
Cc: "'musl@lists.openwall.com'" <musl@lists.openwall.com>,
	Vasile Iliescu <Vasile.Iliescu@enea.com>
Subject: Re: [musl] errno not set to EBADF when reading from invalid descriptor
Date: Thu, 25 Jun 2020 11:38:04 -0400	[thread overview]
Message-ID: <20200625153803.GO6430@brightrain.aerifal.cx> (raw)
In-Reply-To: <VI1PR03MB4814BDD849D7D6CFBCF4347188920@VI1PR03MB4814.eurprd03.prod.outlook.com>

On Thu, Jun 25, 2020 at 01:21:05PM +0000, Stefan Ciotec wrote:
> Hi,
> 
> We are using MUSL C-library 1.1.22.
> According to the POSIX standard, EOF should be returned and errno
> should be set to EBADF for the read group of functions (i.e.
> fgetc(), getc(), getc_unlocked()) when attempting to read from a
> stream with an invalid file descriptor open for reading.
> However, in our tests with MUSL, we discovered that EOF is returned,
> but errno is not set to EBADF (it's 0 instead), for the following
> code:

I think you're misreading the standard. Per ISO C, it's undefined
behavior to call a read function on a FILE stream not opened for read
or update. The POSIX "shall fail" text you're looking at applies when
the FILE stream is open for read or update but the underlying fd is
not open for reading; this can happen with fdopen, when inheriting an
unsuitable fd for stdin via exec, or when using dup2 to replace
fileno(f) for some already-opened FILE with a reference to a different
open file that was opened for write only.

Note that the POSIX text is not very well aligned with the C text, but
the DESCRIPTION in POSIX refers to "the input stream pointed to by
stream". This reflects that it's a constraint, and passing an
inappropriate stream pointer is a constraint violation.

Note that there are lots of other reasons you can't safely use stdio
read functions on a stream you don't know is suitable for it.
Switching from writing to reading without a successful flush produces
UB, and switching the other direction without a successful seek does,
even if the FILE stream is open for both.

Rich

  reply	other threads:[~2020-06-25 15:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-25 13:21 Stefan Ciotec
2020-06-25 15:38 ` Rich Felker [this message]
2020-06-26  5:14   ` Markus Wichmann
2020-06-26  5:39     ` Damian McGuckin
2020-06-26 16:06     ` Rich Felker

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=20200625153803.GO6430@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=Stefan.Ciotec@enea.com \
    --cc=Vasile.Iliescu@enea.com \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).