Skip to content
Merged
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
41 changes: 40 additions & 1 deletion crates/bashkit/src/builtins/sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ impl Builtin for Sed {
};

let mut output = String::new();
let mut warnings = String::new();
let mut modified_files: Vec<(String, String)> = Vec::new();

for (filename, content) in inputs {
Expand Down Expand Up @@ -890,6 +891,12 @@ impl Builtin for Sed {
}
}

if iterations >= max_iterations {
warnings.push_str(&format!(
"sed: warning: branch/label loop limit ({max_iterations}) reached on line {line_num}; output may be truncated\n"
));
}

hold_space = state.hold_space;

// Insert text comes before the line
Expand Down Expand Up @@ -947,7 +954,16 @@ impl Builtin for Sed {
}
}

Ok(ExecResult::ok(output))
if warnings.is_empty() {
Ok(ExecResult::ok(output))
} else {
Ok(ExecResult {
stdout: output,
stderr: warnings,
exit_code: 0,
..Default::default()
})
}
}
}

Expand Down Expand Up @@ -1164,4 +1180,27 @@ mod tests {
.unwrap();
assert_eq!(result.stdout, "ax\nbeginy\nmiddley\nendy\nax\n");
}

#[tokio::test]
async fn test_sed_branch_loop_limit_emits_warning() {
// This sed script loops via branch, doubling 'a' each iteration.
// With 1000 iteration limit, it should emit a warning.
let result = run_sed(&[":loop; s/a/aa/; /a\\{2000\\}/!b loop"], Some("a"))
.await
.unwrap();
assert!(
result.stderr.contains("loop limit"),
"expected warning on stderr, got: {}",
result.stderr
);
assert_eq!(result.exit_code, 0);
}

#[tokio::test]
async fn test_sed_normal_branch_no_warning() {
// A simple branch that completes well under the limit
let result = run_sed(&["s/hello/world/"], Some("hello")).await.unwrap();
assert!(result.stderr.is_empty());
assert_eq!(result.stdout, "world\n");
}
}
Loading