uutils / uutils/coreutils

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

Open
#13,184 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

U - ptx
Dominant language
Rust
Stars
24.1k
Forks
2k
Avg merge
1d 5h
Merged PRs (30d)
365

Description

ptx accepts --gap-size/--width (-g/-w) with no upper bound and parses them into usize. A value above isize::MAX (9223372036854775807) aborts the process:

  • debug / overflow-checks on: attempt to subtract with overflow (exit 101).
$ printf 'alpha beta gamma\n' > in
# debug (or release with overflow-checks on)
$ ptx --gap-size 9999999999999999999 in
thread 'main' panicked at src/uu/ptx/src/ptx.rs: attempt to subtract with overflow   # exit 101
  • release (overflow-checks off): the same arithmetic wraps silently and the bad value reaches " ".repeat() / String::with_capacity(), giving capacity overflow or memory allocation … failed (reported in #12787 / #12788).

GNU ptx rejects any value exceeding isize::MAX up front with invalid gap width / invalid line width and exits 1.

$ /usr/bin/ptx --gap-size 9223372036854775807 in   # isize::MAX -> accepted
$ /usr/bin/ptx --gap-size 9223372036854775808 in   # isize::MAX + 1
ptx: invalid gap width: '9223372036854775808'      # exit 1
$ /usr/bin/ptx --width 9223372036854775808 in      # isize::MAX + 1
ptx: invalid line width: ‘9223372036854775808’     # exit 1   

Root cause

gap_size and line_width are unbounded usize fields. The chunk-sizing math then casts those usize values to isize and subtracts
them, e.g. in get_output_chunks:

// src/uu/ptx/src/ptx.rs
let half_line_size = config.line_width / 2;
let max_before_size =
    cmp::max(half_line_size as isize - config.gap_size as isize, 0) as usize;   // <- overflow
...
let max_tail_size =
    cmp::max(max_before_size as isize - before.len() as isize - config.gap_size as isize, 0) as usize;

When a value exceeds isize::MAX, the as isize cast wraps to a negative number (e.g. 9999999999999999999usize as isize == -8446744073709551617). Subtracting that negative value is effectively an addition that overflows isize.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/uu/ptx/src/ptx.rs, focusing on the option handling for --gap-size and --width and the get_output_chunks entry point. Trace how parsed usize values reach the isize arithmetic, then verify that values above isize::MAX are rejected with a nonzero exit while isize::MAX remains accepted. Add or run focused ptx coverage for both options and boundary values.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
64/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.