Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/uu/ptx/src/ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ struct WordRef {
enum PtxError {
#[error("{0}")]
ParseError(ParseIntError),
#[error("invalid gap width: '{0}'")]
InvalidGapWidth(String),
#[error("invalid line width: '{0}'")]
InvalidLineWidth(String),
}

impl UError for PtxError {}
Expand Down Expand Up @@ -246,20 +250,22 @@ fn get_config(matches: &mut clap::ArgMatches) -> UResult<Config> {
.clone_into(&mut config.trunc_str);
}
if matches.contains_id(options::WIDTH) {
config.line_width = matches
.get_one::<String>(options::WIDTH)
.expect(err_msg)
.parse()
.map_err(PtxError::ParseError)?;
let s = matches.get_one::<String>(options::WIDTH).expect(err_msg);
let v: usize = s.parse().map_err(PtxError::ParseError)?;
if v > isize::MAX as usize {
return Err(PtxError::InvalidLineWidth(s.clone()).into());
}
config.line_width = v;
} else if matches.get_flag(options::TYPESET_MODE) {
config.line_width = 100;
}
if matches.contains_id(options::GAP_SIZE) {
config.gap_size = matches
.get_one::<String>(options::GAP_SIZE)
.expect(err_msg)
.parse()
.map_err(PtxError::ParseError)?;
let s = matches.get_one::<String>(options::GAP_SIZE).expect(err_msg);
let v: usize = s.parse().map_err(PtxError::ParseError)?;
if v > isize::MAX as usize {
return Err(PtxError::InvalidGapWidth(s.clone()).into());
}
config.gap_size = v;
}
if let Some(format) = matches.get_one::<String>(options::FORMAT) {
config.format = match format.as_str() {
Expand Down
22 changes: 22 additions & 0 deletions tests/by-util/test_ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,25 @@ fn test_invalid_regex_word_trailing_backslash() {
fn test_invalid_regex_word_unclosed_group() {
new_ucmd!().args(&["-W", "(wrong"]).succeeds().no_stderr();
}

#[test]
fn test_gap_size_above_isize_max_is_rejected() {
// Values exceeding isize::MAX cause arithmetic overflow in the output
// chunk sizing; GNU ptx rejects them up front (#13184).
let too_big = format!("{}", isize::MAX as u64 + 1);
new_ucmd!()
.args(&["--gap-size", &too_big])
.pipe_in("hello world\n")
.fails()
.stderr_contains("invalid gap width");
}

#[test]
fn test_line_width_above_isize_max_is_rejected() {
let too_big = format!("{}", isize::MAX as u64 + 1);
new_ucmd!()
.args(&["--width", &too_big])
.pipe_in("hello world\n")
.fails()
.stderr_contains("invalid line width");
}
Loading