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 |
||
Email |
Stefan.Ciotec@enea.com |
|
Phone |
+40764758162 |
|
|
||
|
||
|