diff --git a/src/log/syslog.rs b/src/log/syslog.rs index 86eff0554..10df1fcd9 100644 --- a/src/log/syslog.rs +++ b/src/log/syslog.rs @@ -51,14 +51,20 @@ impl Write for SysLogWriter { if self.cursor + message.len() > LIMIT { // floor_char_boundary is currently unstable let mut truncate_boundary = LIMIT - self.cursor; - while !message.is_char_boundary(truncate_boundary) { + while truncate_boundary > 0 && !message.is_char_boundary(truncate_boundary) { truncate_boundary -= 1; } + // don't overzealously truncate log messages truncate_boundary = message[..truncate_boundary] .rfind(|c: char| c.is_ascii_whitespace()) .unwrap_or(truncate_boundary); + if truncate_boundary == 0 { + // we failed to find a "nice" cut off point, abruptly cut off the msg + truncate_boundary = LIMIT - self.cursor; + } + let left = &message[..truncate_boundary]; let right = &message[truncate_boundary..]; diff --git a/test-framework/e2e-tests/src/lib.rs b/test-framework/e2e-tests/src/lib.rs index a9f98cbdf..3d9008f26 100644 --- a/test-framework/e2e-tests/src/lib.rs +++ b/test-framework/e2e-tests/src/lib.rs @@ -1,6 +1,7 @@ #![cfg(test)] mod pty; +mod regression; mod su; type Error = Box; diff --git a/test-framework/e2e-tests/src/regression.rs b/test-framework/e2e-tests/src/regression.rs new file mode 100644 index 000000000..967aaf75a --- /dev/null +++ b/test-framework/e2e-tests/src/regression.rs @@ -0,0 +1,17 @@ +use sudo_test::{Command, Env, TextFile}; + +use crate::Result; + +#[test] +fn syslog_writer_should_not_hang() -> Result<()> { + let env = Env(TextFile("ALL ALL=(ALL:ALL) NOPASSWD: ALL").chmod("644")).build()?; + + let stdout = Command::new("sudo") + .args(["env", "CC=clang-18", "CXX=clang++-18", "FOO=\"........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\"", "whoami"]) + .output(&env)? + .stdout()?; + + assert_eq!(stdout, "root"); + + Ok(()) +}