From dd893cc4b1c176cceabce02fcc98a14fd18d84d7 Mon Sep 17 00:00:00 2001 From: Luan Taraschi <130802253+luantaraschi@users.noreply.github.com> Date: Thu, 26 Nov 2026 19:25:49 -0300 Subject: [PATCH 1/2] sed: keep &&, ~~, -- and escaped \ or [ literal in bracket expressions The regex crate reads &&, ~~ and -- inside a class as set operations and \ and [ as syntax. Escape them when they are ordinary members, whether typed or written as an escape such as \x5c. --- src/sed/delimited_parser.rs | 53 +++++++++++++++++++++++++++++++++++-- tests/by-util/test_sed.rs | 23 ++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/sed/delimited_parser.rs b/src/sed/delimited_parser.rs index 8d089fe5..35e4211a 100644 --- a/src/sed/delimited_parser.rs +++ b/src/sed/delimited_parser.rs @@ -241,6 +241,35 @@ pub fn parse_char_escape( Ok(decoded) } +/// Add an ordinary member to a bracket expression. Inside a class the regex crate +/// reads `\`, `[`, `&&`, `~~` and `--` as syntax, while POSIX sees ordinary +/// characters, so they are escaped. For two dashes in a row the one escaped is the +/// range end: the second in `[!--]`, or the first in `[--x]`, where it opens the class. +fn push_class_byte( + result: &mut Vec, + byte: u8, + members_start: usize, + raw_dash_end: &mut Option, +) { + match byte { + b'\\' | b'[' | b'&' | b'~' => result.extend_from_slice(&[b'\\', byte]), + b'-' if *raw_dash_end == Some(result.len()) => { + if result.len() == members_start + 1 { + result.insert(members_start, b'\\'); + result.push(b'-'); + *raw_dash_end = Some(result.len()); + } else { + 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. @@ -269,6 +298,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']'); @@ -348,7 +380,13 @@ 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 @@ -356,7 +394,12 @@ fn parse_character_class( 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(); } } @@ -918,6 +961,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(); diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index a4e7450f..8b304790 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -692,6 +692,29 @@ 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")] +#[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. From d7d325bfc86a47eec464bda01eb2f4b164463c0a Mon Sep 17 00:00:00 2001 From: Luan Taraschi <130802253+luantaraschi@users.noreply.github.com> Date: Sat, 28 Nov 2026 17:29:30 -0300 Subject: [PATCH 2/2] sed: escape a leading - when it is pushed, and test [---] and [^--x] --- src/sed/delimited_parser.rs | 16 ++++------------ tests/by-util/test_sed.rs | 2 ++ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/sed/delimited_parser.rs b/src/sed/delimited_parser.rs index 35e4211a..7a8bcf5a 100644 --- a/src/sed/delimited_parser.rs +++ b/src/sed/delimited_parser.rs @@ -241,10 +241,8 @@ pub fn parse_char_escape( Ok(decoded) } -/// Add an ordinary member to a bracket expression. Inside a class the regex crate -/// reads `\`, `[`, `&&`, `~~` and `--` as syntax, while POSIX sees ordinary -/// characters, so they are escaped. For two dashes in a row the one escaped is the -/// range end: the second in `[!--]`, or the first in `[--x]`, where it opens the class. +/// 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, byte: u8, @@ -253,14 +251,8 @@ fn push_class_byte( ) { match byte { b'\\' | b'[' | b'&' | b'~' => result.extend_from_slice(&[b'\\', byte]), - b'-' if *raw_dash_end == Some(result.len()) => { - if result.len() == members_start + 1 { - result.insert(members_start, b'\\'); - result.push(b'-'); - *raw_dash_end = Some(result.len()); - } else { - result.extend_from_slice(br"\-"); - } + b'-' if result.len() == members_start || *raw_dash_end == Some(result.len()) => { + result.extend_from_slice(br"\-"); } b'-' => { result.push(b'-'); diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index 8b304790..55cb3d9e 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -700,6 +700,8 @@ fn test_bracket_literal_open_bracket(#[values(r"s/[\[]/X/g", r"s/[[]/X/g")] scri #[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")] +#[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")]