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
16 changes: 14 additions & 2 deletions src/uu/tr/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,18 @@ impl Sequence {
.map(|(l, a)| (l, Ok(Self::CharStar(a))))
}

// `[c*N]` only ever needs to be as long as the other set it is padding

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need 9 lines of comments for this

// out to match, which -- since it comes from a single command-line
// argument -- cannot realistically exceed a few MiB. A literal N well
// beyond that (nothing stops someone writing `[a*999999999999]`) still
// produces the exact same translation as this cap does, since every
// position past the other set's real length collapses to the same
// "pad with the last element" rule regardless of how much further past
// it N actually reaches; not capping it means materializing N literal
// bytes downstream, which is either an out-of-memory abort or, for a
// merely huge rather than astronomical N, a multi-second hang.
const MAX_CHAR_REPEAT: usize = 2 * 1024 * 1024;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should catch actual allocation's error instead of adding such bound. Or it panics when RAM is actually lacking.


fn parse_char_repeat(input: &[u8]) -> IResult<&[u8], Result<Self, BadSequence>> {
delimited(
tag("["),
Expand All @@ -611,13 +623,13 @@ impl Sequence {
let result = if cnt_str.starts_with(b"0") {
match usize::from_str_radix(&s, 8) {
Ok(0) => Ok(Self::CharStar(c)),
Ok(count) => Ok(Self::CharRepeat(c, count)),
Ok(count) => Ok(Self::CharRepeat(c, count.min(Self::MAX_CHAR_REPEAT))),
Err(_) => Err(BadSequence::InvalidRepeatCount(s.to_string())),
}
} else {
match s.parse::<usize>() {
Ok(0) => Ok(Self::CharStar(c)),
Ok(count) => Ok(Self::CharRepeat(c, count)),
Ok(count) => Ok(Self::CharRepeat(c, count.min(Self::MAX_CHAR_REPEAT))),
Err(_) => Err(BadSequence::InvalidRepeatCount(s.to_string())),
}
};
Expand Down
26 changes: 26 additions & 0 deletions tests/by-util/test_tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,32 @@ fn tr_translate_overlap_repeat_squeeze() {
.stdout_is("BCx");
}

#[test]
fn tr_huge_repeat_count_does_not_crash_or_hang() {
// An explicit [c*N] repeat count far larger than any set actually
// needs (here, a trillion) must not be taken literally: the value
// used to be materialized into an N-byte Vec, which is either an
// out-of-memory abort for an N this size or, for a merely huge
// rather than astronomical N, a multi-second hang -- while every N
// beyond what the other set actually needs produces the identical
// translation regardless of its exact size.
new_ucmd!()
.args(&["[a*999999999999]", "x"])
.pipe_in("abc")
.succeeds()
.stdout_is("xbc");
new_ucmd!()
.args(&["a", "[x*999999999999]"])
.pipe_in("abc")
.succeeds()
.stdout_is("xbc");
new_ucmd!()
.args(&["-d", "[a*999999999999]"])
.pipe_in("abc")
.succeeds()
.stdout_is("bc");
}

#[test]
fn octal_repeat_count_test() {
//below will result in 8'x' and 4'y' as octal 010 = decimal 8
Expand Down
Loading