Repository navigation
fix(printf): apply flags, width and precision to numbers - #225
Merged
ferdingler merged 6 commits intoNov 12, 2026
Merged
Conversation
Collaborator
Author
|
@strandly-the-agent please review |
strandly-the-agent
approved these changes
Nov 12, 2026
Collaborator
There was a problem hiding this comment.
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' 1e400givesinf(bash:1e+400), and%eof1e-400gives0.000000e+00. Worth one more word in the description, nothing to change in code. - Don't add
%#gcases near a rounding carry (%#G 999999.5) as differential tests. Bash prints1.E+06, which is a glibc 2.36 bug. A compiled C program on the same box prints the same thing. Your1.00000E+06is the C-correct output.
What I verified
- Head:
e69724c.cargo test --test shell_integration --test differential: 1588 + 1 passed.cargo fmt --checkis clean.cargo clippy -D warnings(Rust 1.99) reports nothing inprintf.rs. Its errors are all inexec.rs,find.rs,lua.rsandls.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%#gcarry bug above. A separate pass of about 4.5k cases againstbash -cmatched, apart from long-double cases and those same glibc cases. - Hand probes, all matching bash:
%5.2f|%03d,%.0fof 0.5, 1.5 and 2.5 (gives0|2|2),%gat 1e5, 1e6, 1e-4 and 1e-5,%#x/%#oof 0,%+u,%x -1,% 05d,%08.3d,%-+08.3f,%010f inf,-nan, and format reuse. - Huge fields:
%99999999999.99999999999{f,e,g},%.4294967296f,%.18446744073709551616fand%#.65535gall 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/%ucame out as literal text and%03dprinted7). Theknown-differencemarker onprintf_zero_pad.shis 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)
%cstill ignores width (%5c xgivesx). Passing it throughpad()would fix that.%skeeps its own width parser atprintf.rs:106-142, which has noMAX_FIELDclamp. That is the source of the huge-width hang you already noted. Moving%sontoparse_specwould 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
This was referenced Nov 13, 2026
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 parsed flags, width and precision but ignored them for numbers, and
%f,%e,%gand%uweren't implemented at all. So you'd get wrong output with no error:Now
d i u o x Xhonour the-,0,+, space and#flags, width and precision (minimum digits for integers), andf F e E g Gformat like C/bash. A width or precision bigger than 65535 is clamped, so something like%99999999999dcan'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:%swith 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.
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.