Skip to content

fix(printf): apply flags, width and precision to numbers - #225

Merged
ferdingler merged 6 commits into
strands-agents:mainfrom
ferdingler:fix/printf-number-padding
Nov 12, 2026
Merged

ferdingler merged 6 commits into
strands-agents:mainfrom
ferdingler:fix/printf-number-padding

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

printf parsed flags, width and precision but ignored them for numbers, and %f, %e, %g and %u weren't implemented at all. So you'd get wrong output with no error:

$ printf '%5.2f|%03d\n' 3.14159 7
%f|7        # bash:  3.14|007

Now d i u o x X honour the -, 0, +, space and # flags, width and precision (minimum digits for integers), and f F e E g G format like C/bash. A width or precision bigger than 65535 is clamped, so something like %99999999999d can't take down the shell. How arguments are parsed is unchanged on purpose (0x10, 'A, error messages for bad numbers), I'll do that in a later PR. This touches the same file as #215 in an adjacent spot, the conflict is trivial to resolve.

I compared about 80 cases against bash's builtin printf and they're in a table-driven test, along with regression cases for %s, %b, %c, %% and format reuse, plus tests for the huge-width clamp. Floats beyond ~17 significant digits differ from bash because bash uses long double. One follow-up: %s with a huge width still hangs, that was already the case on main.

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
ferdingler marked this pull request as ready for review November 9, 2026 23:52
@ferdingler
ferdingler requested a review from a team as a code owner November 9, 2026 23:52
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent please review

@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. I found no correctness bugs. The number formatting matched C/bash on everything I tried, including about 150k fuzzed cases, and the 65535 clamp holds without panics or hangs.

@ferdingler, two small things, neither blocking:

  • The description says floats differ from bash only past about 17 significant digits. They also differ on range, because we parse to f64: printf '%g' 1e400 gives inf (bash: 1e+400), and %e of 1e-400 gives 0.000000e+00. Worth one more word in the description, nothing to change in code.
  • Don't add %#g cases near a rounding carry (%#G 999999.5) as differential tests. Bash prints 1.E+06, which is a glibc 2.36 bug. A compiled C program on the same box prints the same thing. Your 1.00000E+06 is the C-correct output.
What I verified
  • Head: e69724c. cargo test --test shell_integration --test differential: 1588 + 1 passed. cargo fmt --check is clean. cargo clippy -D warnings (Rust 1.99) reports nothing in printf.rs. Its errors are all in exec.rs, find.rs, lua.rs and ls.rs, which this PR doesn't touch.
  • Differential fuzz: about 150k random flag × width × precision × conversion × value combinations (raw f64 bit patterns, rounding ties, carries like 9.9999995 and 99999.95, i64 min/max, inf, nan, -0) checked against a compiled C printf. There were 47 mismatches, and every one is the glibc %#g carry bug above. A separate pass of about 4.5k cases against bash -c matched, apart from long-double cases and those same glibc cases.
  • Hand probes, all matching bash: %5.2f|%03d, %.0f of 0.5, 1.5 and 2.5 (gives 0|2|2), %g at 1e5, 1e6, 1e-4 and 1e-5, %#x/%#o of 0, %+u, %x -1, % 05d, %08.3d, %-+08.3f, %010f inf, -nan, and format reuse.
  • Huge fields: %99999999999.99999999999{f,e,g}, %.4294967296f, %.18446744073709551616f and %#.65535g all return quickly at the clamped length. Clamping to 65535 differs from bash at widths ≥65536, which is the intended trade-off.
  • Tests: without the change, the table test fails (%f/%u came out as literal text and %03d printed 7). The known-difference marker on printf_zero_pad.sh is correctly removed.

Questions (non-blocking, for the #207 follow-up): * width/precision and length modifiers are still unhandled. printf '%*d|%ld|' 5 3 7 prints %*d|%ld|%*d|%ld|, while bash prints 3|7|. Agents do write %ld. Do those belong in the argument-parsing PR?

Appendix: pre-existing, non-blocking (2)

  • %c still ignores width (%5c x gives x). Passing it through pad() would fix that.
  • %s keeps its own width parser at printf.rs:106-142, which has no MAX_FIELD clamp. That is the source of the huge-width hang you already noted. Moving %s onto parse_spec would fix it, but it would need a char-count-aware pad.

Evidence: s3://strandly-prod-artifacts-319426459652/strands-agents/shell/pr/225/ (cargo-test.log, fuzz2.py, fuzz2-diffs.tsv, huge-field-results.txt)

# Conflicts:
#	src/builtins/printf.rs
@ferdingler
ferdingler merged commit d4c57a7 into strands-agents:main Nov 12, 2026
30 checks passed
@ferdingler
ferdingler deleted the fix/printf-number-padding branch November 12, 2026 18:00
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