Skip to content

Commit 867c535

Browse files
committed
fix: avoid panic on multibyte char boundary in log message truncation
1 parent fd93934 commit 867c535

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/daemon.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ pub fn init_background_tracing(
141141
let formatted = format!("{value:?}");
142142
const MAX_MESSAGE_CHARS: usize = 280;
143143
if formatted.len() > MAX_MESSAGE_CHARS {
144-
write!(writer, "{}={}", field_name, &formatted[..MAX_MESSAGE_CHARS])?;
144+
let boundary = formatted.floor_char_boundary(MAX_MESSAGE_CHARS);
145+
write!(writer, "{}={}", field_name, &formatted[..boundary])?;
145146
write!(writer, "...")
146147
} else {
147148
write!(writer, "{}={formatted}", field_name)
@@ -201,7 +202,8 @@ pub fn init_foreground_tracing(
201202
let formatted = format!("{value:?}");
202203
const MAX_MESSAGE_CHARS: usize = 280;
203204
if formatted.len() > MAX_MESSAGE_CHARS {
204-
write!(writer, "{}={}", field_name, &formatted[..MAX_MESSAGE_CHARS])?;
205+
let boundary = formatted.floor_char_boundary(MAX_MESSAGE_CHARS);
206+
write!(writer, "{}={}", field_name, &formatted[..boundary])?;
205207
write!(writer, "...")
206208
} else {
207209
write!(writer, "{}={formatted}", field_name)

0 commit comments

Comments
 (0)