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
19 changes: 14 additions & 5 deletions src/sed/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ fn compile_address_range(
}

// 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 `,'");
}
Expand Down Expand Up @@ -497,8 +499,10 @@ fn compile_address(
break;
}
match line.current() {
'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();
Expand Down Expand Up @@ -1480,11 +1484,16 @@ fn compile_number_command(
lines: &mut ScriptLineProvider,
line: &mut ScriptCharProvider,
cmd: &mut Command,
_context: &mut ProcessingContext,
context: &mut ProcessingContext,
) -> UResult<CommandHandling> {
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);
Expand Down Expand Up @@ -1871,7 +1880,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,
}),
Expand Down
39 changes: 39 additions & 0 deletions tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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", "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])
.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!()
Expand Down
Loading