Skip to content

Commit

Permalink
tr: forbid non-numeric repeat counts
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewliebenow committed Oct 12, 2024
1 parent 3997428 commit 96bff67
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/uu/tr/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use crate::unicode_table;
use nom::{
branch::alt,
bytes::complete::{tag, take},
character::complete::{digit1, one_of},
bytes::complete::{tag, take, take_till},
character::complete::one_of,
combinator::{map, map_opt, peek, recognize, value},
multi::{many0, many_m_n},
sequence::{delimited, preceded, separated_pair},
Expand Down Expand Up @@ -404,7 +404,14 @@ impl Sequence {
fn parse_char_repeat(input: &[u8]) -> IResult<&[u8], Result<Self, BadSequence>> {
delimited(
tag("["),
separated_pair(Self::parse_backslash_or_char, tag("*"), digit1),
separated_pair(
Self::parse_backslash_or_char,
tag("*"),
// TODO
// Why are the opening and closing tags not sufficient?
// Backslash check is a workaround for `check_against_gnu_tr_tests_repeat_bs_9`
take_till(|ue| matches!(ue, b']' | b'\\')),
),
tag("]"),
)(input)
.map(|(l, (c, cnt_str))| {
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,3 +1512,12 @@ fn test_backwards_range() {
",
);
}

#[test]
fn test_non_digit_repeat() {
new_ucmd!()
.args(&["a", "[b*c]"])
.pipe_in("")
.fails()
.stderr_only("tr: invalid repeat count 'c' in [c*n] construct\n");
}

0 comments on commit 96bff67

Please sign in to comment.