Skip to content
Open
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
21 changes: 13 additions & 8 deletions src/sed/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,14 @@ fn compile_text_command_gnu(
// True after a \ at the end of a line
let mut escaped_newline = false;

if line.eol() {
return compilation_error(
lines,
line,
format!("command `{}' expects \\ followed by text", cmd.code),
);
}

// Skip optional \.
if !line.eol() && line.current() == '\\' {
line.advance();
Expand Down Expand Up @@ -2797,19 +2805,16 @@ mod tests {
}

#[test]
fn test_compile_text_command_gnu_optional_backslash_eol_eof() {
fn test_compile_text_command_gnu_no_text() {
let mut chars = make_char_provider("a");
let mut lines = make_line_provider(&[]);
let mut cmd = Command::default();
let mut context = ProcessingContext::default();

compile_text_command(&mut lines, &mut chars, &mut cmd, &mut context).unwrap();
match &cmd.data {
CommandData::Text(text) => {
assert_eq!(text.to_string(), "\n");
}
_ => panic!("Expected CommandData::Text"),
}
let result = compile_text_command(&mut lines, &mut chars, &mut cmd, &mut context);
assert!(result.is_err());
let err = result.unwrap_err().to_string();
assert!(err.contains("expects \\ followed by text"));
}

#[test]
Expand Down
11 changes: 11 additions & 0 deletions tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,17 @@ fn test_incomplete_test_command_posix() {
.stderr_is("sed: :0:3: error: incomplete command\n");
}

#[test]
fn test_empty_text_commands_fail() {
for command in ["a", "c", "i"] {
new_ucmd!()
.args(&["-e", command])
.fails()
.code_is(1)
.stderr_contains(format!("command `{command}' expects \\ followed by text"));
}
}

#[test]
fn test_addr0_non_posix() {
new_ucmd!()
Expand Down
Loading