fix: reject empty text commands

This commit is contained in:
mukunda katta
2026-05-15 13:11:51 -07:00
parent f083fa0634
commit 142e70bd13
2 changed files with 24 additions and 8 deletions
+13 -8
View File
@@ -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]
+11
View File
@@ -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!()