mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] fputs/fputc doesn't set errno on failure
@ 2020-10-08 14:55 John Scott
  2020-10-08 16:21 ` Rich Felker
  2020-10-08 16:23 ` Markus Wichmann
  0 siblings, 2 replies; 4+ messages in thread
From: John Scott @ 2020-10-08 14:55 UTC (permalink / raw)
  To: musl

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

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

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

* Re: [musl] fputs/fputc doesn't set errno on failure
  2020-10-08 14:55 [musl] fputs/fputc doesn't set errno on failure John Scott
@ 2020-10-08 16:21 ` Rich Felker
  2020-10-08 16:23 ` Markus Wichmann
  1 sibling, 0 replies; 4+ messages in thread
From: Rich Felker @ 2020-10-08 16:21 UTC (permalink / raw)
  To: musl; +Cc: John Scott

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

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

* Re: [musl] fputs/fputc doesn't set errno on failure
  2020-10-08 14:55 [musl] fputs/fputc doesn't set errno on failure John Scott
  2020-10-08 16:21 ` Rich Felker
@ 2020-10-08 16:23 ` Markus Wichmann
  2020-10-08 19:55   ` Szabolcs Nagy
  1 sibling, 1 reply; 4+ messages in thread
From: Markus Wichmann @ 2020-10-08 16:23 UTC (permalink / raw)
  To: John Scott; +Cc: musl

On Thu, Oct 08, 2020 at 10:55:47AM -0400, John Scott wrote:
> Hi,
>
> I'm not subscribed, please keep me CC'd.
>

Here's hoping I chose the correct option this time. I went with "group
reply" but directed the message to you, which should CC the list.

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

Interesting. The error occurs because f is only open for reading, so
__towrite() delivers an error without setting errno. Though my POSIX
manpage does not say which error to return for this. EBADF speaks about
the FD underlying the stream, and in this case that one happens to be
the case, but the error occurs solely because F_NOWR is set in the file.
The FD does not come into play. On the other hand, using fdopen() such
that the mode given to open() and the string given to fdopen() are
discordant is undefined behavior. So we might as well assume that F_NOWR
means O_RDONLY. Besides, no other documented error on that page really
fits this case.

I suggest adding error codes to the error paths in __towrite() and
__toread() (which has the same problem).

> 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

It would appear Szabolcs' repo is broken. I cannot access .../git/, but
accessing .../ yields a landing page that references the non-working
pages given here. I managed to find a mirror here:

https://repo.or.cz/libc-test.git

But that one also reference Szabolcs' repo as the original.

Ciao,
Markus

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

* Re: [musl] fputs/fputc doesn't set errno on failure
  2020-10-08 16:23 ` Markus Wichmann
@ 2020-10-08 19:55   ` Szabolcs Nagy
  0 siblings, 0 replies; 4+ messages in thread
From: Szabolcs Nagy @ 2020-10-08 19:55 UTC (permalink / raw)
  To: Markus Wichmann; +Cc: John Scott, musl

* Markus Wichmann <nullplan@gmx.net> [2020-10-08 18:23:34 +0200]:
> On Thu, Oct 08, 2020 at 10:55:47AM -0400, John Scott wrote:
> 
> > 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
> 
> It would appear Szabolcs' repo is broken. I cannot access .../git/, but
> accessing .../ yields a landing page that references the non-working
> pages given here. I managed to find a mirror here:

sorry about that, i fixed it now.

(that website went through a migration that was left incomplete, now fixed)

> 
> https://repo.or.cz/libc-test.git
> 
> But that one also reference Szabolcs' repo as the original.

git clone worked, but the gitweb browser did not.

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

end of thread, other threads:[~2020-10-08 19:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-08 14:55 [musl] fputs/fputc doesn't set errno on failure John Scott
2020-10-08 16:21 ` Rich Felker
2020-10-08 16:23 ` Markus Wichmann
2020-10-08 19:55   ` Szabolcs Nagy

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