Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Thu, 8 Oct 2020 12:21:16 -0400
From: Rich Felker <dalias@...c.org>
To: musl@...ts.openwall.com
Cc: John Scott <jscott@...teo.net>
Subject: Re: fputs/fputc doesn't set errno on failure

On Thu, Oct 08, 2020 at 10:55:47AM -0400, John Scott wrote:
> Hi,
> 
> I'm not subscribed, please keep me CC'd.
> 
> In this corner case, errno is unset despite fputs giving an error.
> I'm on Debian testing using musl-gcc 1.2.1.
> 
> #define _POSIX_C_SOURCE 200809L
> #include <assert.h>
> #include <errno.h>
> #include <stdio.h>
> int main(void) {
> 	FILE *f = fopen("/", "r");
> 	if(f == NULL) {
> 		perror("fopen()");
> 	}
> 	if(fputs("Hello world\n", f) == EOF) {
> 		assert(errno);
> 		perror("fputs()");
> 	}
> }
> 
> With glibc this prints EBADF for fputs.
> 
> The wiki page about writing tests [1], which I thought this would be
> a good candidate for, has the 404 link
> http://nsz.repo.hu/git/?p=libc-test
> 
> Please let me know if I can help debugging.
> 
> [1] https://wiki.musl-libc.org/writing-tests.html

The test is invoking undefined behavior by calling an output function
on a stream not opened for write or update mode. If you instead opened
"/" with O_RDONLY but then passed it to fdopen with mode "r+" or
similar, the POSIX-specified "shall fail" for "The file descriptor
underlying stream is not a valid file descriptor open for writing"
would apply and EBADF would be set (assuming fdopen didn't take the
liberty to fail here, which POSIX allows but does not require and musl
does not do since it would make fdopen more expensive).

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.