Skip to content

numfmt: don't panic on a multi-byte suffix under a multi-byte-separat… - #14065

Open
nagendramohan wants to merge 3 commits into
uutils:mainfrom
nagendramohan:fix/numfmt-multibyte-suffix-panic-13937
Open

nagendramohan wants to merge 3 commits into
uutils:mainfrom
nagendramohan:fix/numfmt-multibyte-suffix-panic-13937

Conversation

@nagendramohan

Copy link
Copy Markdown
Contributor

…or locale

Under a locale whose decimal separator is multi-byte (e.g. ar_SA.UTF-8, where it is the Arabic '٫', U+066B, 2 bytes), an input like '1٫€K' aborted with a char-boundary panic in find_valid_number_with_suffix.

The numeric part was iterated with s.chars().skip(numeric_part.len()) — using the byte length as a character count — which skipped past the multi-byte separator incorrectly, and the result was then sliced with &s[..=numeric_part.len()], landing inside a multi-byte suffix character.

Iterate the remainder from the numeric part's byte offset (a valid char boundary, since it is a prefix of the input) and slice using each suffix character's actual UTF-8 length. Malformed input is now rejected as GNU does ('invalid suffix in input'), and valid multi-byte-separator input such as '1٫5K' still parses.

Adds a regression test (verified failing before the fix). Closes #13937.

Closes #13937.

Under a locale whose decimal separator is multi-byte (e.g. ar_SA.UTF-8, where it's the Arabic ٫, U+066B, 2 bytes), an input like 1٫€K aborted with a char-boundary panic:

$ LC_ALL=ar_SA.UTF-8 numfmt --from=si '1٫€K'
thread 'main' panicked at src/uu/numfmt/src/format.rs:70:20:
byte index 4 is not a char boundary; it is inside '€' (bytes 3..6) of 1٫€K

Root cause: the numeric part was iterated with s.chars().skip(numeric_part.len()) — using its byte length as a character count — so a multi-byte separator was mis-skipped, and the result was then sliced with &s[..=numeric_part.len()], landing inside a multi-byte suffix char.

Fix: iterate the remainder from the numeric part's byte offset (a valid char boundary) and slice using each suffix char's actual len_utf8(). Malformed input is now rejected like GNU (invalid suffix in input), and valid multi-byte-separator input like 1٫5K still parses.

Adds a regression test (verified failing before the fix). cargo fmt, cargo clippy, and the full numfmt test suite pass.

@oech3

oech3 commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Duplicated PR. Please close this.

@github-actions

github-actions Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/tail/pipe-f2. tests/tail/pipe-f2 is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/tail/retry (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/cp/link-heap is now being skipped but was previously passing.

@codspeed

codspeed Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 22 untouched benchmarks
⏩ 464 skipped benchmarks1


Comparing nagendramohan:fix/numfmt-multibyte-suffix-panic-13937 (7f96f39) with main (ab69b86)2

Open in CodSpeed

Footnotes

  1. 464 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (0289758) during the generation of this report, so ab69b86 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@RenjiSann

Copy link
Copy Markdown
Collaborator

@oech3 please mention which PR it is duplicated from (here #13938)

@nagendramohan

Copy link
Copy Markdown
Contributor Author

@RenjiSann Can help with review here :)

Comment thread src/uu/numfmt/src/format.rs Outdated
let accepts_i = [Unit::Auto, Unit::Iec(true)].contains(&unit);

let mut characters = s.chars().skip(numeric_part.len());
// Look at the characters immediately after the numeric part. `numeric_part` is a prefix of

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One Line Comment Please :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done — shortened both the code and test comments to one line each. Thanks!

Sorry this was old PR with long comment :(

@nagendramohan
nagendramohan force-pushed the fix/numfmt-multibyte-suffix-panic-13937 branch from b3efcbe to 284cbe4 Compare September 3, 2026 07:04
@sylvestre

Copy link
Copy Markdown
Contributor

Fix looks correct; two nits: the len_utf8() arithmetic at lines 71/74 is always 1 (suffixes are ASCII, see RawSuffix::try_from) and the line-60 comment should blame the multi-byte decimal separator rather than the suffix; also s.chars().nth(valid_part.len()) at line 180 has the same byte-vs-char confusion and could use the same s[valid_part.len()..].chars().next() fix.

@nagendramohan

Copy link
Copy Markdown
Contributor Author

Thanks! Addressed all three: dropped the always-1 len_utf8() math, fixed the comment to blame the multi-byte separator, and applied the same byte-vs-char fix at line 180. Also rebased onto latest main.

@nagendramohan

Copy link
Copy Markdown
Contributor Author

@sylvestre the panic still reproduces on current main (format.rs:94). This PR still applies cleanly and has a regression test. Could you take another look?

…or locale

Under a locale whose decimal separator is multi-byte (e.g. ar_SA.UTF-8,
where it is the Arabic '٫', U+066B, 2 bytes), an input like '1٫€K'
aborted with a char-boundary panic in find_valid_number_with_suffix.

The numeric part was iterated with s.chars().skip(numeric_part.len()) —
using the *byte* length as a *character* count — which skipped past the
multi-byte separator incorrectly, and the result was then sliced with
&s[..=numeric_part.len()], landing inside a multi-byte suffix character.

Iterate the remainder from the numeric part's byte offset (a valid char
boundary, since it is a prefix of the input) and slice using each suffix
character's actual UTF-8 length. Malformed input is now rejected as GNU
does ('invalid suffix in input'), and valid multi-byte-separator input
such as '1٫5K' still parses.

Adds a regression test (verified failing before the fix). Closes uutils#13937.

Signed-off-by: Nagendra Mohan <nagendramohan1990@gmail.com>
- Drop always-1 len_utf8() math (valid suffixes are ASCII).
- Comment now blames the multi-byte separator, not the suffix.
- Fix the same byte-vs-char slip in the error path (was chars().nth on a byte length).
@sylvestre
sylvestre force-pushed the fix/numfmt-multibyte-suffix-panic-13937 branch from 2638dc2 to 7f96f39 Compare November 27, 2026 20:50

This branch has not been deployed

No deployments
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.

numfmt panics (str char-boundary) on a number using a multibyte locale decimal separator before a multibyte char and a suffix

4 participants