From 689d78fa6acf7e83ff740c122f0796259adb974f Mon Sep 17 00:00:00 2001 From: Nadir Ali <36660597+Xnadir@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:18:28 +0500 Subject: [PATCH 1/2] sed: with --posix, reject Q, q exit codes, addr,+N and the I/M modifiers These are GNU extensions, and GNU sed --posix refuses them when the script is compiled. Leave `l N` to #345 and #552, which already reject it. Closes #650 Co-Authored-By: Claude Opus 5.5 --- src/sed/compiler.rs | 18 ++++++++++++++++-- tests/by-util/test_sed.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/sed/compiler.rs b/src/sed/compiler.rs index 0d9bda36..b5c4cdce 100644 --- a/src/sed/compiler.rs +++ b/src/sed/compiler.rs @@ -375,6 +375,11 @@ fn compile_address_range( return compilation_error(lines, line, "~step is invalid in POSIX mode"); } + // addr1,+N is a GNU extension. + if context.posix && !line.eol() && line.current() == '+' { + return compilation_error(lines, line, "unexpected `,'"); + } + // Look for second address; a missing ~step counts as 0. let has_addr2 = !line.eol() && (is_address_char(line.current()) || line.current() == '+'); if !(has_addr2 || is_step_match || is_step_end) { @@ -497,6 +502,10 @@ fn compile_address( break; } match line.current() { + // The I and M modifiers are GNU extensions. + c @ ('I' | 'M') if context.posix => { + return compilation_error(lines, line, format!("unknown command: `{c}'")); + } 'I' => icase = true, 'M' => multiline = true, _ => break, @@ -1480,11 +1489,16 @@ fn compile_number_command( lines: &mut ScriptLineProvider, line: &mut ScriptCharProvider, cmd: &mut Command, - _context: &mut ProcessingContext, + context: &mut ProcessingContext, ) -> UResult { line.advance(); // Skip the command character line.eat_spaces(); // Skip any leading whitespace + // An exit code after q is a GNU extension. + if context.posix && cmd.code == 'q' && !line.eol() && line.current().is_ascii_digit() { + return compilation_error(lines, line, "extra characters after command"); + } + match parse_number(lines, line, false)? { Some(n) => { cmd.data = CommandData::Number(n); @@ -1871,7 +1885,7 @@ fn get_cmd_spec( handler: compile_number_command, }), // Q is a GNU extension - 'Q' => Ok(CommandSpec { + 'Q' if !posix => Ok(CommandSpec { n_addr: 1, handler: compile_number_command, }), diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index e7ad2ef0..1fa48d50 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -1293,6 +1293,45 @@ fn test_line_address_beyond_usize() { .stdout_is("r\n"); } +#[test] +fn test_posix_rejects_gnu_commands_and_addresses() { + for (script, error) in [ + ("1,+1p", "unexpected `,'"), + ("/a/,+2p", "unexpected `,'"), + ("Q", "invalid command code `Q'"), + ("Q7", "invalid command code `Q'"), + ("q5", "extra characters after command"), + ("$q 5", "extra characters after command"), + ("/A/Ip", "unknown command: `I'"), + ("/a/Mp", "unknown command: `M'"), + ("1,/A/Ip", "unknown command: `I'"), + ] { + new_ucmd!() + .args(&["--posix", "-n", "-e", script]) + .pipe_in("a\nb\n") + .fails_with_code(1) + .no_stdout() + .stderr_contains(error); + } + // Without --posix they are accepted, and a plain q is POSIX. + for (args, output) in [ + (&["-n", "1,+1p"][..], "a\nb\n"), + (&["-n", "/A/Ip"][..], "a\n"), + (&["Q"][..], ""), + ] { + new_ucmd!() + .args(args) + .pipe_in("a\nb\n") + .succeeds() + .stdout_is(output); + } + new_ucmd!() + .args(&["--posix", "q"]) + .pipe_in("a\nb\n") + .succeeds() + .stdout_is("a\n"); +} + #[test] fn test_posix_disables_gnu_character_classes() { new_ucmd!() From f91ba91eab1702d75c886ac7a8fe36b675c9db7c Mon Sep 17 00:00:00 2001 From: Nadir Ali <36660597+Xnadir@users.noreply.github.com> Date: Fri, 20 Nov 2026 04:01:36 +0500 Subject: [PATCH 2/2] sed: simplify the --posix checks and don't write input sed never reads Review follow-up. The +N check is part of has_addr2, and I/M are left to the command parser under --posix ("invalid command code"), like Q. The test no longer pipes input to scripts sed rejects before reading it, which failed with a broken pipe on Linux and macOS. Co-Authored-By: Claude Opus 5.5 --- src/sed/compiler.rs | 19 +++++++------------ tests/by-util/test_sed.rs | 8 ++++---- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/sed/compiler.rs b/src/sed/compiler.rs index b5c4cdce..c4534606 100644 --- a/src/sed/compiler.rs +++ b/src/sed/compiler.rs @@ -375,13 +375,10 @@ fn compile_address_range( return compilation_error(lines, line, "~step is invalid in POSIX mode"); } - // addr1,+N is a GNU extension. - if context.posix && !line.eol() && line.current() == '+' { - return compilation_error(lines, line, "unexpected `,'"); - } - // Look for second address; a missing ~step counts as 0. - let has_addr2 = !line.eol() && (is_address_char(line.current()) || line.current() == '+'); + // addr1,+N is a GNU extension. + let has_addr2 = !line.eol() + && (is_address_char(line.current()) || (line.current() == '+' && !context.posix)); if !(has_addr2 || is_step_match || is_step_end) { return compilation_error(lines, line, "unexpected `,'"); } @@ -502,12 +499,10 @@ fn compile_address( break; } match line.current() { - // The I and M modifiers are GNU extensions. - c @ ('I' | 'M') if context.posix => { - return compilation_error(lines, line, format!("unknown command: `{c}'")); - } - 'I' => icase = true, - 'M' => multiline = true, + // The I and M modifiers are GNU extensions: with --posix + // they are left to the command parser, which rejects them. + 'I' if !context.posix => icase = true, + 'M' if !context.posix => multiline = true, _ => break, } line.advance(); diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index 1fa48d50..a4e7450f 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -1302,13 +1302,13 @@ fn test_posix_rejects_gnu_commands_and_addresses() { ("Q7", "invalid command code `Q'"), ("q5", "extra characters after command"), ("$q 5", "extra characters after command"), - ("/A/Ip", "unknown command: `I'"), - ("/a/Mp", "unknown command: `M'"), - ("1,/A/Ip", "unknown command: `I'"), + ("/A/Ip", "invalid command code `I'"), + ("/a/Mp", "invalid command code `M'"), + ("1,/A/Ip", "invalid command code `I'"), ] { + // No input: sed stops at the script, before it reads any. new_ucmd!() .args(&["--posix", "-n", "-e", script]) - .pipe_in("a\nb\n") .fails_with_code(1) .no_stdout() .stderr_contains(error);