Thanks for the testcase. This looks like a small flaw in newlib.
I redirected to the newlib list.
I debugged your testcase and the problem appears to be in __srefill(),
defined in newlib/libc/stdio/refill.c:
/* if not already reading, have to be reading and writing */
if ((fp->_flags & __SRD) == 0)
{
if ((fp->_flags & __SRW) == 0)
return EOF;
So, what happens is that EOF is returned if the file is not readable.
Errno isn't set and the error condition on the file pointer isn't set
either.
Testing the same situation on Linux, errno is set to EBADF and the
error indicator is set on the file pointer, while the EOF condition
stays clear.
So, I'd like to propose the below patch. I assume a similar patch
should be ok for __sfvwrite, too, isn't it? There's a call to
cantwrite() which only returns EOF but which probably should also
set the error condition and errno.