Repository navigation
Conversation
|
GNU testsuite comparison: |
913d39e to
9885a71
Compare
|
|
||
| // 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 |
There was a problem hiding this comment.
could you please make this comment shorter? one line is enough
| // 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() { |
There was a problem hiding this comment.
could be let Some(&s) = suffixes.get(i) else { return Err(...) }; no?
| } | ||
|
|
||
| #[test] | ||
| fn test_to_si_rounding_past_top_suffix_is_too_big() { |
There was a problem hiding this comment.
please add the same case with --to=iec too
|
Thanks, I've applied all three suggestions. |
Merging this PR will regress 1 benchmark
|
| 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)
Footnotes
-
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. ↩
| } | ||
|
|
||
| #[test] | ||
| fn test_to_si_top_suffix_value_still_renders() { |
There was a problem hiding this comment.
could be merged into test_rounding_past_top_suffix, no?
There was a problem hiding this comment.
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.
cdeb795 to
7cfc632
Compare
There was a problem hiding this comment.
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.
Fixes #12581.
Problem
numfmt --to=si/--to=iecpanics with an index-out-of-bounds for a value in the top decade whose scaled magnitude rounds up to the next base.consider_suffixbuildssuffixes = [K, M, G, T, P, E, Z, Y, R, Q](length 10, indices 0..=9), but the index match can yieldi == 10forabs_n ∈ [bases[10], bases[10]*1000). Normally the scaled value then falls belowbases[1]and theelsebranch (suffixes[i - 1]= the top suffix) is taken. But when rounding lifts the scaled value to>= bases[1](e.g.999.5 -> 1000with the default--round=from-zero), the promote-to-next-suffix branch runssuffixes[i]withi == 10— out of bounds.With the default
from-zerorounding the affected range is the whole top decade(≈9.99e32, 1e33);--round=down/towards-zeroare unaffected.Fix
Treat "rounded past the largest suffix" as too large and return the existing
numfmt-error-number-too-bigerror (exit 2) — the same result numfmt already gives for>= bases[10]*1000. Values that stay within the top suffix are unaffected:(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) andtest_to_si_top_suffix_value_still_renders(--round=downstill yields999Q). Full numfmt suite passes;cargo fmt/clippyclean.