stat: fix overflow on out-of-range octal escape - #14371
Merged
Merged
Conversation
A \NNN escape whose value exceeds 255 (\400 through \777) was accumulated in a u8, so the multiply overflowed and a build with overflow-checks panicked with exit code 101. Accumulate the escape in a u32 and keep the low byte, so \400 prints 0x00 and \777 prints 0xFF, matching GNU.
|
GNU testsuite comparison: |
Contributor
|
Thanks for your PR! |
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.
What was wrong
stat --printf '\400'(and any\NNNescape from\400through\777) aborted instead of printing a byte:Any build with overflow-checks enabled (debug builds by default, or release with
-C overflow-checks) hits this.Root cause
handle_escape_sequencesaccumulated the octal escape in au8:Three octal digits can encode values up to 511, so as soon as the accumulated value passes
u8::MAXthe multiply overflows.The fix
Accumulate in a
u32— three octal digits reach at most 511, so the arithmetic can no longer overflow — and keep the low byte when building theToken::Byte. That makes\400print0x00and\777print0xFF, matching what GNUstatoutputs for an out-of-range octal escape. In-range escapes are unaffected. Only--printfis affected:generate_tokensinterprets backslash escapes for--printfand treats them literally for-c/--format, matching GNU.The fix was written from scratch; no GNU source was consulted or copied, only GNU's observable output for these escapes.
How it was tested
test_printf_octal_out_of_rangeintests/by-util/test_stat.rsasserts that--printf=\400\777succeeds and emits the bytes00 ff. It fails onmain(the command exits 101) and passes with this change.stattest suite passes:cargo test --no-default-features --features stat --test tests test_stat→ 34 passed, 0 failed.cargo clippy -p uu_stat --no-default-features --all-targetsis clean, andcargo fmtreports no changes.Fixes #14346