diff --git a/src/sed/compiler.rs b/src/sed/compiler.rs index bcfff31..193300b 100644 --- a/src/sed/compiler.rs +++ b/src/sed/compiler.rs @@ -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(); @@ -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] diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index b31fed7..fdc13bd 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -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!()