dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] input: preadfd: read standard input byte-wise
@ 2022-12-13 22:17 наб
  2022-12-13 22:37 ` Harald van Dijk
  0 siblings, 1 reply; 23+ messages in thread
From: наб @ 2022-12-13 22:17 UTC (permalink / raw)
  To: dash

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

POSIX Issue 7, XCU, sh, STDIN says:
  When the shell is using standard input and it invokes a command that
  also uses standard input, the shell shall ensure that the standard
  input file pointer points directly after the command it has read when
  the command begins execution. It shall not read ahead in such a manner
  that any characters intended to be read by the invoked command are
  consumed by the shell (whether interpreted by the shell or not) or
  that characters that are not read by the invoked command are not seen
  by the shell.

I.e.
  sh <<EOF
  id
  cat
  good!
  EOF
must execute id, then execute cat, then the cat must copy "good!"
to the standard output stream, and similarly
  sh <<"EOF"
  id
  read Q
  good!
  echo Q$Q
  EOF
must execute id, then read "good!" into Q, then echo "Qgood!".

Heretofor the output was as such:
  uid=1000(nabijaczleweli) gid=100(users) groups=100(users)
  ./dash: 3: good!: not found
and as such (with -x):
  + id
  uid=1000(nabijaczleweli) gid=100(users) groups=100(users)
  + read Q
  + good!
  sh: 3: good!: not found
  + echo Q
  Q
and a strace confirms:
  read(0, "id\ncat\ngood!\n", 8192)       = 13
  read(0, "id\nread Q\ngood!\necho Q$Q\n", 8192) = 25

Reading the standard input byte-by-byte is the obvious solution to this
issue, Just Works, and is how all other shells do it (we could,
theoretically, read regular files block-wise, then seek within them after
parsing, but the complexity out-weighs the rarity of running
sh < program; we could also do whole-line reads on teletypes in
icanon mode, but, again, the gain here is miniscule for an interactive
session, and the teletype mode can change at any time, so...).
Naturally, we keep reading block-wise for non-standard-input.

With this patch, we observe the correct
  uid=1000(nabijaczleweli) gid=100(users) groups=100(users)
  good!
and
  + id
  uid=1000(nabijaczleweli) gid=100(users) groups=100(users)
  + read Q
  + echo Qgood!
  Qgood!

Fixes: https://bugs.debian.org/862907
---
 src/input.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/input.c b/src/input.c
index ec075f5..6b6113e 100644
--- a/src/input.c
+++ b/src/input.c
@@ -195,7 +195,7 @@ retry:
 
 	} else
 #endif
-		nr = read(parsefile->fd, buf, IBUFSIZ - 1);
+		nr = read(parsefile->fd, buf, parsefile->fd == 0 ? 1 : IBUFSIZ - 1);
 
 
 	if (nr < 0) {
-- 
2.30.2

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

end of thread, other threads:[~2023-01-05  9:44 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-13 22:17 [PATCH] input: preadfd: read standard input byte-wise наб
2022-12-13 22:37 ` Harald van Dijk
2022-12-14  1:05   ` наб
2022-12-14  1:06     ` [PATCH v2 1/3] parser: fixredir: invalid redirections are run-time, not syntax наб
2023-01-05  9:43       ` Herbert Xu
2022-12-14  1:06     ` [PATCH v2 2/3] parser: synerror: explicitly consume the entire invalid line наб
2023-01-03  1:53       ` Herbert Xu
2023-01-03  5:32         ` [PATCH] input: Eat rest of line upon reset Herbert Xu
2023-01-03 11:47         ` [PATCH v2 2/3] parser: synerror: explicitly consume the entire invalid line Harald van Dijk
2023-01-04  9:51           ` Herbert Xu
2023-01-04 11:25             ` Harald van Dijk
2023-01-04 14:10               ` Herbert Xu
2023-01-04 14:30                 ` Harald van Dijk
2023-01-04 14:41                   ` Herbert Xu
2023-01-04 14:59                     ` Harald van Dijk
2023-01-05  7:12                       ` [PATCH] input: Check for int_pending while clearing input Herbert Xu
2022-12-14  1:06     ` [PATCH v2 3/3] input: preadfd: read standard input byte-wise наб
2023-01-03  6:15       ` [v3 PATCH] input: Read " Herbert Xu
2023-01-03 11:54         ` Harald van Dijk
2023-01-04  8:59           ` Herbert Xu
2023-01-04 11:18             ` Harald van Dijk
2023-01-05  7:26               ` [PATCH] input: Only skip blank lines on PS1 Herbert Xu
2023-01-05  8:33                 ` Harald van Dijk

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