mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Lorenzo Beretta <vc.net.loreb@gmail.com>
To: musl@lists.openwall.com
Subject: [musl] request: please detect reads from stdin with unflushed writes to stdout
Date: Mon, 25 Oct 2021 14:28:12 +0200	[thread overview]
Message-ID: <CAAMnvkfEPVNDCOKL8aStaSLcYyPPaVtWt8O8CXUwRHjLt9uPBA@mail.gmail.com> (raw)

[-- 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 --]

             reply	other threads:[~2021-10-25 12:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-25 12:28 Lorenzo Beretta [this message]
2021-10-25 12:44 ` Quentin Rameau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAAMnvkfEPVNDCOKL8aStaSLcYyPPaVtWt8O8CXUwRHjLt9uPBA@mail.gmail.com \
    --to=vc.net.loreb@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).