mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Re: request: please detect reads from stdin with unflushed
@ 2021-10-25 13:17 Lorenzo Beretta
  2021-10-25 17:25 ` Rich Felker
  0 siblings, 1 reply; 3+ messages in thread
From: Lorenzo Beretta @ 2021-10-25 13:17 UTC (permalink / raw)
  To: musl

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

> Suggestion: fix those broken programs!

I know, I know... the problem is that they work with glibc and they fail
silently with musl, and on top of that I've just discovered that at least
netbsd and openbsd do the same as glibc, ie a broken program like
#include <stdio.h>
int main(){
        printf("not flushed: ");
        char line[123];
        return fgets(line,sizeof line,stdin) ? 0 : 123;
}

happens to work!!!

What I'm asking is that musl (while technically correct!) helps
__detecting__ those programs, possibly as an option ("#ifdef
HUMOR_BROKEN_PROGRAMS")

PS
I'm not subscribed to this mailing list, sorry for not mentioning it the
first time

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

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

* Re: [musl] Re: request: please detect reads from stdin with unflushed
  2021-10-25 13:17 [musl] Re: request: please detect reads from stdin with unflushed Lorenzo Beretta
@ 2021-10-25 17:25 ` Rich Felker
  2021-10-25 18:13   ` Lorenzo Beretta
  0 siblings, 1 reply; 3+ messages in thread
From: Rich Felker @ 2021-10-25 17:25 UTC (permalink / raw)
  To: Lorenzo Beretta; +Cc: musl

On Mon, Oct 25, 2021 at 03:17:13PM +0200, Lorenzo Beretta wrote:
> > Suggestion: fix those broken programs!
> 
> I know, I know... the problem is that they work with glibc and they fail
> silently with musl, and on top of that I've just discovered that at least
> netbsd and openbsd do the same as glibc, ie a broken program like
> #include <stdio.h>
> int main(){
>         printf("not flushed: ");
>         char line[123];
>         return fgets(line,sizeof line,stdin) ? 0 : 123;
> }
> 
> happens to work!!!
> 
> What I'm asking is that musl (while technically correct!) helps
> __detecting__ those programs, possibly as an option ("#ifdef
> HUMOR_BROKEN_PROGRAMS")
> 
> PS
> I'm not subscribed to this mailing list, sorry for not mentioning it the
> first time

There's nothing detectable here because there's nothing wrong with the
program; the bug is in the programmer's *expectation* that the output
be visible.

It's possible to implement the behavior the programmer here desired,
the optional flushing of line-buffered output streams before reading
input. This would not help detect the bug in expectaions though; it
would just help mask it. The reason this behavior is not present in
musl is because it does not scale with significant numbers of stdio
streams open, and can even produce deadlock conditions in
multithreaded programs where there is no semantic deadlock but the
additional flushing produces an extraneous operation on a stream in a
way that causes deadlock.

If you hit a program with an issue like this, it should be fairly easy
to fix by adding fflush(stdout) or fflush(0) immediately before the
relevant input operations.

Rich

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

* Re: [musl] Re: request: please detect reads from stdin with unflushed
  2021-10-25 17:25 ` Rich Felker
@ 2021-10-25 18:13   ` Lorenzo Beretta
  0 siblings, 0 replies; 3+ messages in thread
From: Lorenzo Beretta @ 2021-10-25 18:13 UTC (permalink / raw)
  To: Rich Felker; +Cc: musl

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

I was thinking something like a warning on stderr fwiw, but I guess you're
right, I didn't consider multithreading.
Thank you for the detailed reply.

Il giorno lun 25 ott 2021 alle ore 19:25 Rich Felker <dalias@libc.org> ha
scritto:

> On Mon, Oct 25, 2021 at 03:17:13PM +0200, Lorenzo Beretta wrote:
> > > Suggestion: fix those broken programs!
> >
> > I know, I know... the problem is that they work with glibc and they fail
> > silently with musl, and on top of that I've just discovered that at least
> > netbsd and openbsd do the same as glibc, ie a broken program like
> > #include <stdio.h>
> > int main(){
> >         printf("not flushed: ");
> >         char line[123];
> >         return fgets(line,sizeof line,stdin) ? 0 : 123;
> > }
> >
> > happens to work!!!
> >
> > What I'm asking is that musl (while technically correct!) helps
> > __detecting__ those programs, possibly as an option ("#ifdef
> > HUMOR_BROKEN_PROGRAMS")
> >
> > PS
> > I'm not subscribed to this mailing list, sorry for not mentioning it the
> > first time
>
> There's nothing detectable here because there's nothing wrong with the
> program; the bug is in the programmer's *expectation* that the output
> be visible.
>
> It's possible to implement the behavior the programmer here desired,
> the optional flushing of line-buffered output streams before reading
> input. This would not help detect the bug in expectaions though; it
> would just help mask it. The reason this behavior is not present in
> musl is because it does not scale with significant numbers of stdio
> streams open, and can even produce deadlock conditions in
> multithreaded programs where there is no semantic deadlock but the
> additional flushing produces an extraneous operation on a stream in a
> way that causes deadlock.
>
> If you hit a program with an issue like this, it should be fairly easy
> to fix by adding fflush(stdout) or fflush(0) immediately before the
> relevant input operations.
>
> Rich
>

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

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

end of thread, other threads:[~2021-10-25 19:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-25 13:17 [musl] Re: request: please detect reads from stdin with unflushed Lorenzo Beretta
2021-10-25 17:25 ` Rich Felker
2021-10-25 18:13   ` Lorenzo Beretta

Code repositories for project(s) associated with this 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).