From bcc9319cc740e07016474ff2a6d731f262e4871f Mon Sep 17 00:00:00 2001 From: Kanishk Sachan Date: Sat, 20 Jun 2026 11:50:28 +0100 Subject: [PATCH] fix(pr): reject huge --column/-COLUMN counts instead of panicking (fixes #12996) A column count that parses as a valid usize but is enormous (e.g. 9999999999999999999) overflows the Vec allocations in to_table(), to_table_across(), and to_table_short_file(), causing a "capacity overflow" panic and abort (exit 134). Reject column counts above i32::MAX up front with a GNU-style "Value too large for defined data type" error and exit 1, matching GNU pr's behavior. Add regression tests for both the --column and -COLUMN forms. --- src/uu/pr/src/pr.rs | 14 ++++++++++++++ tests/by-util/test_pr.rs | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/uu/pr/src/pr.rs b/src/uu/pr/src/pr.rs index 43df8caca5..1a14701c1a 100644 --- a/src/uu/pr/src/pr.rs +++ b/src/uu/pr/src/pr.rs @@ -858,6 +858,20 @@ fn build_options( None => start_column_option, }; + // Reject column counts that would later overflow the Vec allocations in + // to_table()/to_table_across()/to_table_short_file() (issue #12996), + // matching GNU's "Value too large for defined data type" rejection. + if let Some(columns) = column_option_value { + if columns > i32::MAX as usize { + return Err(PrError::EncounteredErrors { + msg: format!( + "invalid number of columns: {}: Value too large for defined data type", + columns.to_string().quote() + ), + }); + } + } + let column_mode_options = column_option_value.map(|columns| ColumnModeOptions { columns, width: column_width, diff --git a/tests/by-util/test_pr.rs b/tests/by-util/test_pr.rs index dfe42fd811..ccc909d72c 100644 --- a/tests/by-util/test_pr.rs +++ b/tests/by-util/test_pr.rs @@ -942,6 +942,25 @@ fn test_zero_columns_shortcut() { .stderr_contains("pr: invalid --column argument '0'"); } +#[test] +fn test_huge_columns_does_not_panic() { + // Previously panicked with "capacity overflow" (issue #12996). + new_ucmd!() + .arg("--column=9999999999999999999") + .fails_with_code(1) + .stderr_contains("invalid number of columns") + .stderr_contains("Value too large for defined data type"); +} + +#[test] +fn test_huge_columns_shortcut_does_not_panic() { + new_ucmd!() + .arg("-9999999999999999999") + .fails_with_code(1) + .stderr_contains("invalid number of columns") + .stderr_contains("Value too large for defined data type"); +} + #[test] fn test_zero_expand_tab_width() { let expected = "pr: '-e' extra characters or invalid number in the argument: ‘0’\nTry 'pr --help' for more information.\n";