Skip to content
Merged
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
45 changes: 43 additions & 2 deletions src/sed/delimited_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,27 @@ pub fn parse_char_escape(
Ok(decoded)
}

/// Inside a class the regex crate reads `\`, `[`, `&&`, `~~` and `--` as syntax,
/// while POSIX sees ordinary characters, so they are escaped: `[!--]`, `[--x]`.
fn push_class_byte(
result: &mut Vec<u8>,
byte: u8,
members_start: usize,
raw_dash_end: &mut Option<usize>,
) {
match byte {
b'\\' | b'[' | b'&' | b'~' => result.extend_from_slice(&[b'\\', byte]),
b'-' if result.len() == members_start || *raw_dash_end == Some(result.len()) => {
result.extend_from_slice(br"\-");
}
b'-' => {
result.push(b'-');
*raw_dash_end = Some(result.len());
}
_ => result.push(byte),
}
}

/// Parse a POSIX RE character class returning it as bytes.
/// This functionality is needed to avoid terminating delimited
/// sequences when a delimiter appears within a character class.
Expand Down Expand Up @@ -269,6 +290,9 @@ fn parse_character_class(
line.advance();
}

let members_start = result.len();
let mut raw_dash_end = None;

// Optional leading ']' inside the class
if !line.eol() && line.current() == ']' {
result.push(b']');
Expand Down Expand Up @@ -348,15 +372,26 @@ fn parse_character_class(
parse_char_escape(lines, line)?
};
if let Some(decoded) = decoded {
push_script_char(&mut result, decoded, character_mode);
// A decoded character is a member like a typed one: GNU sed reads
// `[\x5c]` as a backslash and `[\x26\x26]` as an ampersand.
if decoded.is_ascii() {
push_class_byte(&mut result, decoded as u8, members_start, &mut raw_dash_end);
} else {
push_script_char(&mut result, decoded, character_mode);
}
} else {
// Outside the character escapes a \ is an ordinary member of the set,
// so the next character is left for the loop to handle. This is what
// makes `[a\]bc]` end at the first `]`, as GNU sed does.
result.extend_from_slice(br"\\");
}
} else {
result.push(line.current_byte());
push_class_byte(
&mut result,
line.current_byte(),
members_start,
&mut raw_dash_end,
);
line.advance();
}
}
Expand Down Expand Up @@ -918,6 +953,12 @@ mod tests {
#[case::collating_symbol("[[.ch.]]", b"[[.ch.]]")]
#[case::malformed_posix_like_pattern_treated_as_literal("[[x]yz]", br"[\[x]")]
#[case::literal_open_bracket_in_character_class("[a[b]", br"[a\[b]")]
#[case::ampersand_and_tilde_are_escaped("[a&&~~]", br"[a\&\&\~\~]")]
#[case::range_ending_in_dash("[!--]", br"[!-\-]")]
#[case::range_starting_with_dash("[--x]", br"[\--x]")]
#[case::range_from_dash_to_dash("[---]", br"[\--\-]")]
#[case::negated_range_starting_with_dash("[^--x]", br"[^\--x]")]
#[case::escaped_backslash("[\\x5c]", br"[\\]")]
fn test_parse_character_class(#[case] input: &str, #[case] expected: &[u8]) {
let mut line = ScriptCharProvider::new(input);
let lines = test_lines();
Expand Down
25 changes: 25 additions & 0 deletions tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,31 @@ fn test_bracket_literal_open_bracket(#[values(r"s/[\[]/X/g", r"s/[[]/X/g")] scri
.stdout_is("aXb\n");
}

// Inside a class the regex crate reads &&, ~~ and -- as set operations, but in a
// bracket expression they are ordinary members, and so are a \ or [ written as
// an escape.
#[rstest]
#[case::double_ampersand(r"s/[a&&b]/X/g", "a&b\n", "XXX\n")]
#[case::double_tilde(r"s/[~~]/X/g", "c~d\n", "cXd\n")]
#[case::range_ending_in_dash(r"s/[!--]/X/g", "d-e&\n", "dXeX\n")]
#[case::range_starting_with_dash(r"s/[--x]/X/g", "-x.\n", "XXX\n")]

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.

please add [---] and [^--x] here too, they are only covered by the unit tests

#[case::range_from_dash_to_dash(r"s/[---]/X/g", "a-b\n", "aXb\n")]
#[case::negated_range_starting_with_dash(r"s/[^--x]/X/g", "a-b,c\n", "a-bXc\n")]
#[case::escaped_backslash(r"s/[\x5c]/\//g", "C:\\Users\\me\n", "C:/Users/me\n")]
#[case::escaped_open_bracket(r"s/[\x5b]/X/g", "a[b\n", "aXb\n")]
#[case::escaped_ampersands(r"s/[\x26\x26]/X/g", "a&b\n", "aXb\n")]
fn test_bracket_set_operators_are_literal(
#[case] script: &str,
#[case] input: &str,
#[case] expected: &str,
) {
new_ucmd!()
.args(&["-e", script])
.pipe_in(input)
.succeeds()
.stdout_is(expected);
}

#[test]
fn test_bracket_backslash_b_is_not_backspace() {
// sed has no \b character escape, so [\b] holds a backslash and a b.
Expand Down
Loading