Skip to content

numfmt: don't panic on a value that rounds past the largest suffix - #12582

Open
leeewee wants to merge 1 commit into
uutils:mainfrom
leeewee:numfmt-fix-suffix-index-overflow
Open

leeewee wants to merge 1 commit into
uutils:mainfrom
leeewee:numfmt-fix-suffix-index-overflow

Conversation

@leeewee

@leeewee leeewee commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #12581.

Problem

numfmt --to=si / --to=iec panics with an index-out-of-bounds for a value in the top decade whose scaled magnitude rounds up to the next base.

$ numfmt --to=si 999500000000000000000000000000000
thread 'main' panicked at src/uu/numfmt/src/format.rs:536:33:
index out of bounds: the len is 10 but the index is 10
$ echo $?
134

consider_suffix builds suffixes = [K, M, G, T, P, E, Z, Y, R, Q] (length 10, indices 0..=9), but the index match can yield i == 10 for abs_n ∈ [bases[10], bases[10]*1000). Normally the scaled value then falls below bases[1] and the else branch (suffixes[i - 1] = the top suffix) is taken. But when rounding lifts the scaled value to >= bases[1] (e.g. 999.5 -> 1000 with the default --round=from-zero), the promote-to-next-suffix branch runs suffixes[i] with i == 10 — out of bounds.

With the default from-zero rounding the affected range is the whole top decade (≈9.99e32, 1e33); --round=down/towards-zero are unaffected.

Fix

Treat "rounded past the largest suffix" as too large and return the existing numfmt-error-number-too-big error (exit 2) — the same result numfmt already gives for >= bases[10]*1000. Values that stay within the top suffix are unaffected:

$ numfmt --to=si 1000000000000000000000000000000     # 1.0Q  (unchanged)
$ numfmt --to=si 999000000000000000000000000000000   # 999Q  (unchanged)
$ numfmt --to=si --round=down 999500000000000000000000000000000   # 999Q (unchanged)
$ numfmt --to=si 999500000000000000000000000000000   # now: "Number is too big and unsupported", exit 2

(For reference, GNU coreutils 9.4 prints the placeholder 1.0(error) with exit 0 in this case — a known limitation; this errors cleanly instead.)

Tests

Added test_to_si_rounding_past_top_suffix_is_too_big (errors, exit 2) and test_to_si_top_suffix_value_still_renders (--round=down still yields 999Q). Full numfmt suite passes; cargo fmt/clippy clean.

@github-actions

github-actions Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/misc/io-errors (fails in this run but passes in the 'main' branch)

@leeewee
leeewee force-pushed the numfmt-fix-suffix-index-overflow branch from 913d39e to 9885a71 Compare June 4, 2026 04:53
Comment thread src/uu/numfmt/src/format.rs Outdated

// check if rounding pushed us into the next base
if v.abs() >= bases[1] {
// Rounding promoted the value to the next base. When already at the

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.

could you please make this comment shorter? one line is enough

Comment thread src/uu/numfmt/src/format.rs Outdated
// Rounding promoted the value to the next base. When already at the
// largest base there is no suffix above it, so the value is too large
// to represent (rather than indexing `suffixes` out of bounds).
if i >= suffixes.len() {

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.

could be let Some(&s) = suffixes.get(i) else { return Err(...) }; no?

Comment thread tests/by-util/test_numfmt.rs Outdated
}

#[test]
fn test_to_si_rounding_past_top_suffix_is_too_big() {

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.

please add the same case with --to=iec too

Copilot AI balanced review requested due to automatic review settings October 4, 2026 12:32

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@leeewee

leeewee commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, I've applied all three suggestions.

@codspeed

codspeed Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will regress 1 benchmark

⚠️ 119 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

ℹ️ 1 benchmark has no flame graph

The flamegraph could not be generated because too much time was spent in runtime internals, making the visualization not useful for performance analysis.

⚡ 2 improved benchmarks
❌ 1 regressed benchmark
✅ 398 untouched benchmarks
⏩ 70 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Simulation factor_three_39_bit_primes 567 ms 644.7 ms -12.06%
⚡ Simulation factor_five_38_bit_primes 1.6 s 1.5 s +11.16%
⚡ Simulation factor_thirteen_39_bit_primes 9.5 s 8.7 s +9.36%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing leeewee:numfmt-fix-suffix-index-overflow (cdeb795) with main (7654e67)

Open in CodSpeed

Footnotes

  1. 70 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. ↩

Comment thread tests/by-util/test_numfmt.rs Outdated
}

#[test]
fn test_to_si_top_suffix_value_still_renders() {

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.

could be merged into test_rounding_past_top_suffix, no?

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.

Merged, thanks

Copilot AI balanced review requested due to automatic review settings October 15, 2026 07:04

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

consider_suffix indexes suffixes[i] with i up to 10 on a 10-element array.
For a value in the top decade ([bases[10], bases[10]*1000)) whose scaled
value rounds up to >= bases[1], i == 10 takes the promote-to-next-suffix
branch and indexes suffixes[10] -> index out of bounds -> abort.

e.g. `numfmt --to=si 999500000000000000000000000000000` aborted (exit 134).

Report "Number is too big and unsupported" (exit 2) instead, matching
numfmt's existing handling of larger values. Values that stay within the
top suffix (e.g. with --round=down) still render as 999Q.
@leeewee
leeewee force-pushed the numfmt-fix-suffix-index-overflow branch from cdeb795 to 7cfc632 Compare December 1, 2026 23:13
Copilot AI balanced review requested due to automatic review settings December 1, 2026 23:13

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

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 (index out of bounds) on a very large --to=si/--to=iec value

3 participants