Repository navigation
fix: match bash on head -c, exec redirects, and read -d - #307
Conversation
head -c took fd 0 out of the process and dropped the leftover bytes, so x=$(head -c 1); cat discarded the rest of stdin. exec < file with no command hit a TODO and left stdin unchanged. read did not parse -d; an empty delimiter is NUL, as in bash.
|
thanks for the follow up! |
|
@strandly-the-agent please review |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changes requested. The head -c and exec <file fixes hold up. But the no-command exec now applies output redirects as well, and that breaks exec 2>&1 and exec >log, both of which are no-ops on main. read -d also trims more than bash does.
- 🔴
exec 2>&1/exec 3>&1fail withbad fd 1(rc 1, andset -eaborts).exec >fmakes all later output disappear and leavesfempty. - 🟡
read -dtrims non-whitespace IFS characters, trimsREPLY, and strips\r.IFS= read -r -d '' x < filedrops the trailing newline (root cause filed as #308). - 🟡
read -rd '', the usual spelling of this idiom, still doesn't parse.
@ferdingler, for you as requester.
✅ What I verified
- Head
b7a7c87(basef5c6ac4).cargo test --test shell_integration: 1970 passed, including the 3 new tests. - Compared against bash 5.2.15 and coreutils 9.1 in the sandbox. Held:
x=$(head -c 1); cat, sequentialhead -c 1,-c 0,-clarger than the input,-c -N;exec < f+while read,exec <<EOF,exec </dev/nullin functions and groups,( exec >f ); echo b(no leak),exec </nonexistent(continues, as bash does);read -d ,with field splitting, NUL records,-d é(first byte, same as bash),-dwith no argument → rc 2.
Appendix: non-blocking (5)
- ⚪
head -n Nstill loses the rest of stdin when stdin is a file:BufReaderreads ahead and that buffer is dropped before the fd is restored.{ head -n 1; cat; } < fprints only the first line; bash prints all of it. Pipes match bash, and this isn't a regression (mainlost fd 0 entirely), but the comment athead.rs:104-106is only accurate for-c. - ⚪ The redirect-expansion
matchnow appears five times inexec.rs(:1790,:2130,:2436,:3106,:3402). A small helper would remove the duplication. The IFS lookup is also duplicated inread.rs:89/:109. - ⚪ Without
-r, a backslash-escaped delimiter still ends the read:read -d , xona\,b,givesa\(bash givesa,b). This is part of the existing lack of backslash handling inread. - ⚪
read -d ""(double quotes) turns intoread -d xbecause of #299. The test uses'', so it passes. - ⚪ Pre-existing, not from this PR, filed as #308: builtins ignore prefix assignments, so
IFS=, read a bsplits on the shell's IFS.
…ead -d No-command exec applied output redirects to the session process, which has no fd 1 or 2, so exec 2>&1 failed with bad fd 1 and exec >file swallowed later output. Only stdin redirects are applied now. read -d trims only trailing IFS whitespace, and only when a variable was named; it keeps CR and leaves REPLY untrimmed. Combined short flags (-rd, -d:) are parsed.
|
@strandly-the-agent Please review this PR |
There was a problem hiding this comment.
Approve. All three findings from my last review are fixed at 2a82009, and I found nothing new that blocks. @ferdingler
- 🔴 no-command
execoutput redirects: fixed.exec 2>&1,exec >&2,exec 3>&1andexec >fare no-ops again, andset -e; exec 2>&1; echo afterprintsafter. - 🟡
-dtrimming: fixed. The first three rows of the table match bash. The fourth matches whenIFS=is its own statement; the prefix formIFS= read -r -d '' xstill waits on #308. - 🟡
read -rd ''/-d:: fixed.
One behaviour change to be aware of: an unsupported flag inside a cluster (read -rs, -rn1, -rt 5, -ra) now fails with invalid option and rc 2. On main these silently assigned to the wrong variable, so I read this as an improvement.
What I verified, and non-blocking notes
Verified at 2a82009 (bash 5.2.15 as reference, main @ ad54fd1 as baseline)
cargo test --workspace --all-targets: exit 0, no failures;shell_integration2058 passed, including the 11 tests this PR adds. CI is green. I did not runcargo fmtorclippylocally.- 57 one-off scripts run through
strands-shell -candbash -c. Matching bash:read -rd '',-d:,-d ' ',read -r --, NUL-separatedwhile IFS= read -rd '' floops, multi-byte input before a delimiter,read -d '' x < file;exec < filefollowed by awhile readloop, inside a function, a subshell,$(…)and a pipeline stage,exec 0< f,exec <> f,exec <<EOF,exec < missing(rc 1, later commands still run);head -c Ntwice in a row,head -c 0,head -c -2,head -c 2thenread. The ones that differ are the three notes below, plus gaps that are the same onmain(<<<, the-pprompt on a non-terminal). - Read, not run: the merge with #326 (UTF-8 decoding) keeps the timeout path intact, putting the bytes read so far back on fd 0 (
src/builtins/read.rs:109-115).
Appendix: non-blocking (3)
- ⚪
read -s x(an unsupported flag on its own) is still taken as a variable name, whileread -rs xis now an error. Erroring for any unknown option, and accepting-sand-eas no-ops, would make the two consistent. Fine as a follow-up. - ⚪
read -d '' xtrims trailing IFS whitespace but not leading (printf ' a\n'givesa, basha). Plainread xhas never trimmed either side: pre-existing, filed #343, which covers both. - ⚪
IFS=… read -dcases (IFS= read -r -d '' x,IFS='=' read -r -d '' k v) still differ from bash until #308 lands.
Description
ferdingler asked me to re-file this after closing strands-agents/box#442: Box no longer vendors Shell since box#414, and these three gaps were still present upstream, with no open PR covering them.
x=$(head -c 1); catdropped the rest of stdin.headtakes fd 0 out of the process and did not put the remaining reader back, so leftover bytes were discarded with it. After aheadthat read stdin, fd 0 is restored.exec < /dev/nullwith no command returned 0 and left stdin unchanged (there was a TODO for this). Those redirects are now applied to the current shell, so later commands see them.read -d ''did not parse-d. An empty delimiter is NUL, as in bash: the builtin reads until that byte or EOF, assigns what it read, trims trailing IFS, and returns 1 when EOF arrives before the delimiter.I re-checked these on current
mainbefore opening this; the three tests below failed there and pass with the change.Related Issues
Re-filed from strands-agents/box#440 / strands-agents/box#442, at ferdingler's request.
Documentation PR
None.
Type of Change
Bug fix
Testing
cargo test --workspace --all-targets)cargo fmtandcargo clippyThe three new tests failed on unmodified
mainand pass here:head_bytes_in_a_substitution_leaves_the_rest_of_stdinexec_without_a_command_replaces_stdinread_empty_delimiter_takes_all_input_until_eofcargo test --workspace --all-targetswas green (including 1970shell_integrationtests).cargo fmt --checkwas clean.cargo clippy --workspace --all-targetsreported only existing warnings, none in these files.Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.