Skip to content

fix(printf): interpret hex, octal and unicode escapes like bash - #215

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/printf-hex-octal-escapes
Nov 12, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/printf-hex-octal-escapes

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

printf only understood octal escapes that started with \0, and it didn't know \x at all. So printf 'a\x41b\n' printed a\x41b instead of aAb, and printf '\101' printed \101 instead of A. In %b arguments, \0101 came out as a NUL byte followed by 101, and \c didn't stop the output.

Now printf follows bash for \NNN, \xHH, \uHHHH/\UHHHHHHHH, and in %b it also handles \0NNN and \c. Escapes like \xff now 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 \u code 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 \u difference and a few escapes like \a and \e that I left for another change. One existing test expected printf '\0101' to print A, 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.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent please review

@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 04:56
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 04:56

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 run cargo fmt or clippy: neither component is installed in the sandbox.
  • I compared bytes against /usr/bin/bash 5.2 with LC_ALL=C.UTF-8, using od -tx1, on about 60 cases. These all match:
    • \0101 gives 08 31 and \1012 gives A2. \400 gives 00 and \777 gives ff, so the low byte is kept.
    • \x414 gives A4. \xg and a bare \x stay literal. \xff\200 writes raw bytes.
    • \u00e9, \U0001F600 and \u0000 match.
    • A literal \c in the format matches, and so does a trailing \.
    • In %b, \c stops all output, even in the middle of reusing the format for leftover arguments.
    • %s arguments are still printed literally.
  • The changed printf '\0101' test is correct: bash prints \010 followed by 1.
  • I fuzzed the binary with 400 random inputs and it never panicked. I also compiled main's format_string next 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). For printf '\xg', printf '%b' '\x' and printf '\u', bash also writes missing hex digit for \x (or missing unicode digit for \u) to stderr. Stdout and the exit code (0) already match, so only the warning is missing.
  • ⚪ Invalid \u code 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' a prints \%s, where bash prints \a, because the fallback at :253 swallows the next character.
    • %5b ignores the width.
    • %c, %Ns and %.Ns count characters, but bash in C.UTF-8 counts 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.

@ferdingler
ferdingler merged commit 85d2d82 into strands-agents:main Nov 12, 2026
30 of 59 checks passed
@ferdingler
ferdingler deleted the fix/printf-hex-octal-escapes branch November 12, 2026 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants