Skip to content

fix(ptx): reject --gap-size/--width values exceeding isize::MAX#13216

Open
koopatroopa787 wants to merge 1 commit into
uutils:mainfrom
koopatroopa787:fix-ptx-gap-size-width-overflow
Open

fix(ptx): reject --gap-size/--width values exceeding isize::MAX#13216
koopatroopa787 wants to merge 1 commit into
uutils:mainfrom
koopatroopa787:fix-ptx-gap-size-width-overflow

Conversation

@koopatroopa787

Copy link
Copy Markdown
Contributor

Fixes #13184.

Bug

ptx --gap-size N with N > isize::MAX panics (debug) or produces silent garbage / capacity overflow (release) because the output-chunk sizing math casts the value with as isize, which wraps negative on overflow:

let max_before_size =
    cmp::max(half_line_size as isize - config.gap_size as isize, 0) as usize;

GNU ptx rejects any value above isize::MAX up front:

$ ptx --gap-size 9223372036854775808 in
ptx: invalid gap width: '9223372036854775808'   # exit 1
$ ptx --width 9223372036854775808 in
ptx: invalid line width: '9223372036854775808'  # exit 1

Fix

Add two new PtxError variants (InvalidGapWidth, InvalidLineWidth) and validate the parsed usize against isize::MAX as usize immediately after parsing, before the value is stored in Config. This matches GNU's exact error messages and exit code.

Testing

Added test_gap_size_above_isize_max_is_rejected and test_line_width_above_isize_max_is_rejected. All 45 ptx tests pass; cargo clippy -D warnings and cargo fmt --check clean.

Values above isize::MAX are silently cast to a negative isize when the
output-chunk sizing arithmetic does `value as isize`, wrapping in release
builds (producing garbage output or capacity overflow) and panicking in
debug builds (attempt to subtract with overflow).

GNU ptx rejects any such value up front with "invalid gap width" /
"invalid line width" and exits 1. Match that behaviour by validating the
parsed usize against isize::MAX immediately after parsing, before the
value is stored in Config.

Fixes uutils#13184
@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/cut/bounded-memory (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/date/resolution (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/date/date-locale-hour (passes in this run but fails in the 'main' branch)

@codspeed-hq

codspeed-hq Bot commented Jun 30, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 4.67%

❌ 1 regressed benchmark
✅ 326 untouched benchmarks
⏩ 46 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation sort_ascii_c_locale 16 ms 16.7 ms -4.67%

Tip

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


Comparing koopatroopa787:fix-ptx-gap-size-width-overflow (a512fc8) with main (b76d615)

Open in CodSpeed

Footnotes

  1. 46 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.

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.

ptx accepts --gap-size/--width value exceeding isize::MAX while GNU ptx rejects

1 participant