Repository navigation
fix(printf): interpret hex, octal and unicode escapes like bash - #215
Merged
ferdingler merged 2 commits intoNov 12, 2026
Merged
Conversation
This was referenced Nov 6, 2026
Collaborator
Author
|
@strandly-the-agent please review |
ferdingler
marked this pull request as ready for review
November 12, 2026 04:56
strandly-the-agent
approved these changes
Nov 12, 2026
strandly-the-agent
left a comment
Collaborator
There was a problem hiding this comment.
Approve. \NNN, \xHH, \uHHHH/\UHHHHHHHH, %b \0NNN and %b \c match bash 5.2 byte for byte on everything I tried. I found no panics and no regressions, and there's nothing blocking.
One thing to watch is merge order with #231. With this PR, x=$(printf 'a\xffb') gives an empty string, because the capture path (src/exec.rs:3323) throws away the whole stdout when it isn't valid UTF-8. Before this PR you got the literal text a\xffb, which was wrong but not empty. Landing #231 first, or together with this one, avoids a window where printf output silently disappears.
✅ What I verified
- Head
5332d68.cargo test --test shell_integration: 1598 passed (41 of them printf).cargo test --lib: 64 passed, including the 2 new unit tests. I didn't runcargo fmtorclippy: neither component is installed in the sandbox. - I compared bytes against
/usr/bin/bash5.2 withLC_ALL=C.UTF-8, usingod -tx1, on about 60 cases. These all match:\0101gives08 31and\1012givesA2.\400gives00and\777givesff, so the low byte is kept.\x414givesA4.\xgand a bare\xstay literal.\xff\200writes raw bytes.\u00e9,\U0001F600and\u0000match.- A literal
\cin the format matches, and so does a trailing\. - In
%b,\cstops all output, even in the middle of reusing the format for leftover arguments. %sarguments are still printed literally.
- The changed
printf '\0101'test is correct: bash prints\010followed by1. - I fuzzed the binary with 400 random inputs and it never panicked. I also compiled main's
format_stringnext to this PR's version and compared them. On 20k inputs with no backslash, the output was identical. On 3k inputs with backslashes, 128 changed, and in none of those did a result that matched bash stop matching. - Passes: correctness reviewer and adversarial tester. The first adversarial run hit its token limit and I re-ran it. I skipped the API pass because only the internal printf builtin changed and there's no public surface.
Appendix: non-blocking (4)
- ⚪ Missing stderr warnings (
src/builtins/printf.rs:242). Forprintf '\xg',printf '%b' '\x'andprintf '\u', bash also writesmissing hex digit for \x(ormissing unicode digit for \u) to stderr. Stdout and the exit code (0) already match, so only the warning is missing. - ⚪ Invalid
\ucode points are skipped instead of encoded the way bash does. This is documented and tested, so it's fine as is. - ⚪ Bugs that are already on main and that this PR doesn't change. They could go under #207:
printf '\%s' aprints\%s, where bash prints\a, because the fallback at:253swallows the next character.%5bignores the width.%c,%Nsand%.Nscount characters, but bash inC.UTF-8counts bytes.\a,\e,\v,\f,\b,\',\"and\?aren't handled yet.
- ⚪ Capture of invalid UTF-8 (see the TL;DR) is a pre-existing bug and is out of scope here; #231 fixes it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
printf only understood octal escapes that started with
\0, and it didn't know\xat all. Soprintf 'a\x41b\n'printeda\x41binstead ofaAb, andprintf '\101'printed\101instead ofA. In%barguments,\0101came out as a NUL byte followed by101, and\cdidn't stop the output.Now printf follows bash for
\NNN,\xHH,\uHHHH/\UHHHHHHHH, and in%bit also handles\0NNNand\c. Escapes like\xffnow produce the raw byte instead of a UTF-8 re-encoding, so printf can write bytes that aren't valid UTF-8. One small difference from bash: invalid\ucode points (surrogates, anything past U+10FFFF) are skipped, where bash would encode them. echo has its own escape handling and doesn't change.I compared the output byte by byte with bash's printf on about 60 cases, and they match apart from that
\udifference and a few escapes like\aand\ethat I left for another change. One existing test expectedprintf '\0101'to printA, which isn't what bash does, so I fixed the test. A known limit: when output is captured ($(...)or the Shell API), output with invalid UTF-8 is currently dropped entirely. That was already the case before this change, and a separate PR fixes the capture side. fix/printf-numeric-format changes the same function, so the two will conflict, and whichever merges second will need a rebase.Related Issues
Part of #207
Documentation PR
N/A
Type of Change
Bug fix
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
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.