* [musl] request: please detect reads from stdin with unflushed writes to stdout
@ 2021-10-25 12:28 Lorenzo Beretta
2021-10-25 12:44 ` Quentin Rameau
0 siblings, 1 reply; 2+ messages in thread
From: Lorenzo Beretta @ 2021-10-25 12:28 UTC (permalink / raw)
To: musl
[-- Attachment #1: Type: text/plain, Size: 1250 bytes --]
Hello,
there are a few programs around that do something like
printf("question: ");
fgets(ans, sizeof ans, stdin);
without fflushing stdout and get away with it because it happens to work
under glibc.
(fyi the ones I stumbled onto are mkfs.xfs and, vipw/vigr from util-linux,
then the developer noticed the same with chfn/chsh)
Ideally that would be taken care of by either some compiler warning (but
not even clang's -Weverything catches that) or by some static analysis
tool, but I couldn't find any.
A __very__ tentative and untested patch:
diff --git i/src/stdio/__stdio_read.c w/src/stdio/__stdio_read.c
index ea675da3..6b10f76c 100644
--- i/src/stdio/__stdio_read.c
+++ w/src/stdio/__stdio_read.c
@@ -8,6 +8,11 @@ size_t __stdio_read(FILE *f, unsigned char *buf, size_t
len)
{ .iov_base = f->buf, .iov_len = f->buf_size }
};
ssize_t cnt;
+ if (f == stdin) {
+ if (stdout->wpos != stdout->wbase) {
+ do_something(glibc);
+ }
+ }
cnt = iov[0].iov_len ? syscall(SYS_readv, f->fd, iov, 2)
: syscall(SYS_read, f->fd, iov[1].iov_base, iov[1].iov_len);
... with do_something() being either flushing stdout or printing some kind
of warning to stderr (isatty?) or to syslog.
Any suggestion, corrections etc are obviously very welcome.
[-- Attachment #2: Type: text/html, Size: 1572 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [musl] request: please detect reads from stdin with unflushed writes to stdout
2021-10-25 12:28 [musl] request: please detect reads from stdin with unflushed writes to stdout Lorenzo Beretta
@ 2021-10-25 12:44 ` Quentin Rameau
0 siblings, 0 replies; 2+ messages in thread
From: Quentin Rameau @ 2021-10-25 12:44 UTC (permalink / raw)
To: musl
> Hello,
Hi Lorenzo,
> there are a few programs around that do something like
> printf("question: ");
> fgets(ans, sizeof ans, stdin);
>
> without fflushing stdout and get away with it because it happens to work
> under glibc.
> (fyi the ones I stumbled onto are mkfs.xfs and, vipw/vigr from util-linux,
> then the developer noticed the same with chfn/chsh)
>
> Ideally that would be taken care of by either some compiler warning (but
> not even clang's -Weverything catches that) or by some static analysis
> tool, but I couldn't find any.
>
> ... with do_something() being either flushing stdout or printing some kind
> of warning to stderr (isatty?) or to syslog.
> Any suggestion, corrections etc are obviously very welcome.
Suggestion: fix those broken programs!
If one wants to make sure what you print is flushed at a certain point,
ones have to flush the stream.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-10-25 12:44 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-25 12:28 [musl] request: please detect reads from stdin with unflushed writes to stdout Lorenzo Beretta
2021-10-25 12:44 ` Quentin Rameau
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).