mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] errno not set to EBADF when reading from invalid descriptor
@ 2020-06-25 13:21 Stefan Ciotec
  2020-06-25 15:38 ` Rich Felker
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Ciotec @ 2020-06-25 13:21 UTC (permalink / raw)
  To: 'musl@lists.openwall.com'; +Cc: Vasile Iliescu

[-- Attachment #1: Type: text/plain, Size: 2345 bytes --]

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:

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

int main() {
   FILE *f = fopen("/tmp/getc_unlocked", "w");
   char c;
   /* Negative smoke testing. */
   flockfile(f);
   c = getc_unlocked(f);
   printf("c:%#x errno:%d\n", c, errno);
   funlockfile(f);
   fclose(f);
   remove("/tmp/getc_unlocked");
   return 0;
}

I tested the same code with Linux glibc and errno is set appropriately to EBADF.

The issue seems to be in the following portion of the MUSL library:
int __toread(FILE *f)
{
   f->mode |= f->mode-1;
   if (f->wpos != f->wbase) f->write(f, 0, 0);
   f->wpos = f->wbase = f->wend = 0;
   if (f->flags & F_NORD) {
      f->flags |= F_ERR;
      return EOF; // Here errno should be set to EBADF before returning
   }
   f->rpos = f->rend = f->buf + f->buf_size;
   return (f->flags & F_EO
}

Best regards,

Stefan Ciotec
Software Engineer
OS Business Unit
Email
Stefan.Ciotec@enea.com
Phone
+40764758162

Enea
319 Splaiul Independentei
OB403A District 6
Bucharest 060044, ROMANIA

[https://www.enea.com/globalassets/images/enea-email-signature.png]<http://www.enea.com/>



This message, including attachments, is CONFIDENTIAL. It may also be privileged or otherwise protected by law. If you received this email by mistake please let us know by reply and then delete it from your system; you should not copy it or disclose its contents to anyone. All messages sent to and from Enea may be monitored to ensure compliance with internal policies and to protect our business. Emails are not secure and cannot be guaranteed to be error free as they can be intercepted, a mended, lost or destroyed, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of email transmission. Anyone who communicates with us by email accepts these risks.

[-- Attachment #2: Type: text/html, Size: 11559 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-06-26 16:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-25 13:21 [musl] errno not set to EBADF when reading from invalid descriptor Stefan Ciotec
2020-06-25 15:38 ` Rich Felker
2020-06-26  5:14   ` Markus Wichmann
2020-06-26  5:39     ` Damian McGuckin
2020-06-26 16:06     ` Rich Felker

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