Skip to content

pr panics ("Formatting argument out of range") on a large -n width or -W/-w page width #13735

Description

@leeewee

pr builds its line-number column and its page header with format! width arguments taken directly from the -n (number-line width) and -W/--page-width (line width) options, which panics when the resulting width exceeds u16::MAX (65535).

$ printf 'x\n' | pr -n 70000 -
thread 'main' panicked at src/uu/pr/src/pr.rs:1497:13:
Formatting argument out of range
$ echo $?
134

$ printf 'l1\nl2\n' > /tmp/pr_in
$ pr -W 200000 /tmp/pr_in
thread 'main' panicked at src/uu/pr/src/pr.rs:1532:9:
Formatting argument out of range
$ echo $?
134

Root cause

-n line-number width (latest pr.rs:1497, d928f05 :1336): the width comes straight from the parsed -n option and feeds a right-justify format:

// src/uu/pr/src/pr.rs:1491-1497  (get_formatted_line_number)
let num_opt = opts.number.as_ref().unwrap();
let width = num_opt.width;                 // <- from -n DIGITS, unbounded
...
format!("{line_str:>width$}{separator}")   // :1497 — panics when width > u16::MAX

-W/page width (latest pr.rs:1532, d928f05 :1371): the page width flows into the centered-header padding widths:

// src/uu/pr/src/pr.rs:1528-1534  (header layout)
let space_for_filename = total_width - date_len - page_len;      // total_width from -W
let padding_before_filename = (space_for_filename - filename_len) / 2;
let padding_after_filename = space_for_filename - filename_len - padding_before_filename;
format!(                                                          // :1532 — panics when a padding > u16::MAX
    "{date_part}{:padding_before_filename$}{filename}{:padding_after_filename$}{page_part}",
    "", ""
)

In both cases a width/padding above 65535 reaches a format! count and aborts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions