Repository navigation
fix(echo): decode \x, \e and \u escapes and write octal escapes as bytes - #350
ferdingler wants to merge 4 commits into
Conversation
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changes requested. echo -e and printf now match bash 5.2 on every case tested, and the new tests prove the fix. One blocker: plain echo without -e also picks up the five new escapes, and that case matched bash on main. Details and two fix options are in the inline comment.
What I verified
Head 4c822b1, base dbb6189, bash 5.2.15, Linux aarch64, debug builds.
- Suites on head:
cargo test --workspace --all-targetsexits 0 with 2,734 passed, 0 failed, 0 ignored.cargo fmt --checkandcargo clippy --workspace --all-targetsare clean (19 warnings on both base and head, none in the changed files). I did not runpytest tests/pythonornpm test. - Tests prove the fix: with the PR's
tests/on base source, the three new differential cases andecho_esc_unicodefail, and nothing else does. .expectedfiles: re-recording the three cases withenv -i PATH=/usr/bin:/bin HOME=$PWD LC_ALL=C bash case.shgives byte-identical output.- Issue #336: its four
echo -erows andprintf 'A\e[0mB\n'produce bash's bytes. echo -e,printfformat and%b: about 370 hand-written cases and 16,500 random escape strings compared with bash. The only mismatches are invalid code points (\uD800,\U110000), which the code comment documents, and gaps that are the same on base.- No collateral change: 48,000 random inputs without the new escapes give byte-identical output on base and head.
- Raw bytes:
echo -e '\xff' | wc -cis 2 like bash (5 on base). Pipes,>,>>andteekeep the bytes. 47 error-path cases (closed pipe,>&-, read-only file,pipefail,set -e) give the same status and stderr presence on base and head. - Fixed on the way:
echo '\0400'panics on a debug build of base (src/builtins/echo.rs:82: attempt to multiply with overflow, exit 101). Head prints the low byte like bash.
Not tested: release builds, non-Linux platforms, wasm, and the Python and Node bindings.
Appendix: non-blocking (4)
- ⚪
src/builtins/printf.rs:413-420: the three guards are correct in all three modes, but the octal rule is spread over negated tests. Onematch (mode, c)that returns the digit count would read more directly. - ⚪
src/builtins/printf.rs:28: the comment says "printf andecho -eoutput", but plainechoandecho -EuseOuttoo. - ⚪
tests/shell_integration.rs:5466: the headingecho -e escape sequencesstill sits above aprintftest (noted in #211). - ⚪ No test pins
echo -e '\0400', the panic this PR fixes. - Pre-existing, not filed:
{ printf '\xff\n'; } | wc -cprints 4 where bash prints 2, on base and head. Output of a compound command is captured as aString(src/os.rs:287), so invalid bytes become U+FFFD.echo -enow reaches the same path. Not filed because it is the lossy capture chosen in #231.
| expand_escapes(&text) | ||
| let mut out = Out::default(); | ||
| if escapes { | ||
| if escape_all(&text, Escapes::Echo, &mut out) { |
There was a problem hiding this comment.
Plain echo (no -e) now decodes \x, \e, \E, \u and \U too; on main it printed them literally, like bash
escapes defaults to true (line 17), so a flagless echo goes through this same call. The old decoder had no arms for these five escapes, so they came out untouched. Now text that passes through plain echo is rewritten:
| Command | bash and main |
this PR |
|---|---|---|
echo '{"msg":"hi \ud83d\ude00"}' | jq -r .msg |
hi 😀 |
hi (surrogate pair dropped, exit 0) |
echo '{"msg":"say \u0022hi\u0022"}' | jq -r .msg |
say "hi" |
jq parse error, exit 5 |
echo 'C:\xampp\htdocs' |
literal | C:, a newline, mpp\htdocs |
echo 'print("\x1b[31mred\x1b[0m")' |
literal \x1b |
raw ESC bytes |
Plain echo already expanded \n, \t, \\ and \0NNN before this PR (the echo_backslash_literal known-difference), so this widens a known gap. But JSON \u escapes and \x in source code are ordinary things to echo into a file, and the PR description only mentions echo -e.
Suggestion: default to escapes = false on line 17, as bash does. I tried that on this head: 14 integration tests that pin the default expansion fail (echo_esc_*, echo_escape_*, dquote_backslash_nonspecial), and the differential harness asks to remove the marker from echo_backslash_literal.sh. If that's too big for this PR, decode the five new escapes only when -e was given. Either way, add a plain-echo test with these escapes; none exists, so the suite passes both ways.
Reproduction details
Head 4c822b1, base dbb6189 (main), debug builds, Linux aarch64. Oracle: bash 5.2.15. All run as <shell> -c '<command>' with LC_ALL=C.UTF-8, stdout read with od -c. jq is the host's /usr/bin/jq.
$ echo 'A\x41B\e[0m\u00e9'
bash A \ x 4 1 B \ e [ 0 m \ u 0 0 e 9 \n
main A \ x 4 1 B \ e [ 0 m \ u 0 0 e 9 \n
head A A B 033 [ 0 m 303 251 \n
$ echo '{"msg":"hi \ud83d\ude00"}' | jq -r .msg
bash h i 360 237 230 200 \n status 0
main h i 360 237 230 200 \n status 0
head h i \n status 0
$ echo '{"msg":"say \u0022hi\u0022"}' | jq -r .msg
bash s a y " h i " \n status 0
main s a y " h i " \n status 0
head jq: parse error: expected `,` or `}` at line 1 column 14 status 5
$ echo 'C:\xampp\htdocs'
bash C : \ x a m p p \ h t d o c s \n
main C : \ x a m p p \ h t d o c s \n
head C : \n m p p \ h t d o c s \n
$ echo 'print("\x1b[31mred\x1b[0m")'
bash p r i n t ( " \ x 1 b [ 3 1 m r e d \ x 1 b [ 0 m " ) \n
main p r i n t ( " \ x 1 b [ 3 1 m r e d \ x 1 b [ 0 m " ) \n
head p r i n t ( " 033 [ 3 1 m r e d 033 [ 0 m " ) \n
Control: echo -E 'A\x41\e\u00e9' prints the text literally on bash, main and head.
Wider sweep: echo 'P\<c>41Q' for every printable ASCII <c> differs from bash on main for \0 \\ \a \b \c \f \n \r \t \v; head adds exactly \e \E \x \u \U. Of 49 realistic plain-echo commands (JSON, source code, Windows paths, regexes, LaTeX), 40 matched bash on main and no longer do on head.
The surrogate case loses data because src/builtins/printf.rs:455-459 skips code points that aren't valid chars.
Flip experiment: on head with line 17 changed to let mut escapes = false;, cargo test --workspace --all-targets --no-fail-fast fails only those 14 tests in tests/shell_integration.rs and the differential test, whose message is echo_backslash_literal now matches bash: remove the known-difference marker.
There was a problem hiding this comment.
Good catch, thanks. I took the second option. Plain echo with neither -e nor -E now leaves \x, \u, \U, \e and \E literal again, the way it did on main, while echo -e decodes them. Flipping the default to bash's escapes = false is a bigger behavior change, and it breaks the tests that pin the current default, so I left that out of this PR.
Test first in d1f6ff6: a new differential case runs plain echo with these escapes, a JSON \u0022 string and C:\xampp, plus an echo -E control. It failed on 4c822b1 and passes on 6361915. That commit also pins echo -e 'A\0400B' (4 bytes, like bash).
From the appendix: I fixed the Out comment and replaced the negated octal guard with a direct check for \0 or the printf modes. I left the echo -e heading in shell_integration alone since #211 already tracks it.
There was a problem hiding this comment.
Fixed in 6361915: the five repros above now match bash and main, and the new differential case fails on 4c822b1 and passes here.
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. The blocker from my last review is fixed at 6361915: plain echo leaves \x, \u, \U, \e and \E literal again, and echo -e still decodes them. I found nothing new in the delta.
What I verified
Head 6361915, previous head 4c822b1, base dbb6189, bash 5.2.15, Linux aarch64, debug builds.
- Previous repros: all five plain-
echocommands from the inline comment (JSON surrogate pair,\u0022,C:\xampp\htdocs,\x1bin source, the mixed line) now print the same bytes as bash andmain. - Plain
echois back tomain: 2,956 random escape strings throughechoand 984 throughecho -nare byte-identical on base and head. I left out\0NNNvalues above\0177, where base writes two bytes or panics and head writes one. -eand printf are untouched by the delta: 3,000echo -e, 2,000 printf format and 2,000%brandom strings are byte-identical on4c822b1and6361915. 1,579echo -estrings without\u/\Umatch bash.- Flag combinations match bash:
-n,-nE,-E -e,-e -E,-ne,-n -e,-eE,-Ee,-x,--, a leading empty argument,/bin/echo,command echo,builtin echo. - New test proves the fix:
echo_plain_keeps_new_escapes_literalmatches its.expectedon base and6361915and fails on4c822b1. Both touched.expectedfiles are byte-identical to a fresh bash recording underLC_ALL=C. - Suites:
cargo test --workspace --all-targetsexits 0 with 2,734 passed, 0 failed.cargo fmt --checkis clean and clippy reports the same 19 warnings as base, none in the changed files. I did not runpytest tests/pythonornpm test.
Description
echo -edidn't understand\x,\eor\uescapes, so things like\e[31mcolor codes came out as literal text. And an octal escape above\0177came out as two bytes instead of one, soecho -e '\0303\0251'printed mojibake instead ofé. While checking this I found printf had the same gap with\e, plus\a,\b,\fand\v.Now echo uses the same escape decoder as printf, which writes raw bytes and already knew
\xand\u. I added\eand the missing control escapes to that decoder, so printf picks them up too. Echo keeps its own octal rule: only\0NNNis an escape, so\101still prints literally like in bash. The old separate echo decoder is gone. Plainechowithout-estill leaves the new escapes literal, as it did before, so JSON or Windows paths echoed into a file don't change.I added differential cases for the echo and printf escapes and the byte counts, plus one integration test for
\uand\U, because bash in the C locale the differential harness uses prints those literally. On main the three new differential cases and the integration test failed. With the change they pass, and the full test suite passes.Related Issues
Fixes #336
Documentation PR
N/A
Type of Change
Bug fix
Testing
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.