Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(diagnostic): avoid panic slicing config/log at non-UTF-8 boundaries (#3514)

Worktrunk Bot86daf56dfeb8
diff --git a/src/diagnostic.rs b/src/diagnostic.rs
index 23ffc0731..f8de02574 100644
--- a/src/diagnostic.rs
+++ b/src/diagnostic.rs
@@ -431,7 +431,9 @@ fn truncate_log(content: &str) -> String {
return content.to_string();
}
- let start = content.len() - MAX_LOG_SIZE;
+ // Snap to a char boundary before slicing β€” the last ~50KB can begin
+ // mid-character otherwise.
+ let start = content.floor_char_boundary(content.len() - MAX_LOG_SIZE);
// Find the next newline to avoid cutting mid-line
let start = content[start..]
.find('\n')
@@ -525,9 +527,12 @@ fn format_config_section(path: &std::path::Path, kind: ConfigFileKind) -> String
match std::fs::read_to_string(path) {
Ok(content) if content.trim().is_empty() => output.push_str("(empty file)\n"),
Ok(content) => {
- // Include content, but truncate if very long
+ // Include content, but truncate if very long. Snap to a char
+ // boundary so a multi-byte character straddling offset 4000
+ // doesn't panic the slice.
let content = if content.len() > 4000 {
- format!("{}...\n(truncated)", &content[..4000])
+ let end = content.floor_char_boundary(4000);
+ format!("{}...\n(truncated)", &content[..end])
} else {
content
};
@@ -603,6 +608,20 @@ mod tests {
assert!(result.len() < 5000);
}
+ #[test]
+ fn test_format_config_section_truncates_multibyte_at_boundary() {
+ // A multi-byte char straddling the 4000-byte cut must not panic the
+ // slice. "πŸ¦€" is 4 bytes; padding 3998 ASCII bytes puts a crab boundary
+ // at 3998/4002/4006/... so byte 4000 lands mid-character.
+ let tmp = TempDir::new().unwrap();
+ let path = tmp.path().join("emoji.toml");
+ let content = format!("{}{}", "x".repeat(3998), "πŸ¦€".repeat(100));
+ std::fs::write(&path, &content).unwrap();
+
+ let result = format_config_section(&path, ConfigFileKind::User);
+ assert!(result.contains("(truncated)"));
+ }
+
#[test]
fn test_render_trace_profile_summarizes_records() {
let trace = r#"{"kind":"cmd_completed","ts":1000,"tid":1,"context":"main","cmd":"git status","dur_us":12000,"ok":true}
@@ -637,4 +656,15 @@ mod tests {
assert!(result.starts_with("(log truncated to last ~50KB)"));
assert!(result.len() < 55 * 1024);
}
+
+ #[test]
+ fn test_truncate_log_large_content_multibyte_at_boundary() {
+ // The 50KB-from-end offset must not split a multi-byte char. "€" is 3
+ // bytes and 51200 is not a multiple of 3, so a newline-free 3-byte-char
+ // tail forces the cut offset to land mid-character with no '\n' to
+ // realign on β€” the exact case that panicked before the boundary snap.
+ let content = format!("{}{}", "x".repeat(2000), "€".repeat(20000));
+ let result = truncate_log(&content);
+ assert!(result.starts_with("(log truncated to last ~50KB)"));
+ }
}