Skip to content

Commit 90e516b

Browse files
authored
Merge pull request #825 from memorysafety/824-syslog-writer-panic
Fix #824 syslog writer panic
2 parents 208a25b + adffb1e commit 90e516b

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/log/syslog.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ impl Write for SysLogWriter {
5050
loop {
5151
if self.cursor + message.len() > LIMIT {
5252
// floor_char_boundary is currently unstable
53-
let mut mid = LIMIT;
54-
while !message.is_char_boundary(mid) {
55-
mid -= 1;
53+
let mut truncate_boundary = LIMIT - self.cursor;
54+
while !message.is_char_boundary(truncate_boundary) {
55+
truncate_boundary -= 1;
5656
}
5757

58-
mid = message[..mid]
58+
truncate_boundary = message[..truncate_boundary]
5959
.rfind(|c: char| c.is_ascii_whitespace())
60-
.unwrap_or(mid);
60+
.unwrap_or(truncate_boundary);
6161

62-
let left = &message[..mid];
63-
let right = &message[mid..];
62+
let left = &message[..truncate_boundary];
63+
let right = &message[truncate_boundary..];
6464

6565
self.append(left.as_bytes());
6666
self.append(DOTDOTDOT_END);
@@ -105,8 +105,10 @@ impl Log for Syslog {
105105

106106
#[cfg(test)]
107107
mod tests {
108-
use super::Syslog;
109108
use log::Log;
109+
use std::fmt::Write;
110+
111+
use super::{SysLogWriter, Syslog, FACILITY};
110112

111113
#[test]
112114
fn can_write_to_syslog() {
@@ -119,6 +121,15 @@ mod tests {
119121
logger.log(&record);
120122
}
121123

124+
#[test]
125+
fn can_handle_multiple_writes() {
126+
let mut writer = SysLogWriter::new(libc::LOG_DEBUG, FACILITY);
127+
128+
for i in 1..20 {
129+
let _ = write!(writer, "{}", "Test 123 ".repeat(i));
130+
}
131+
}
132+
122133
#[test]
123134
fn can_truncate_syslog() {
124135
let logger = Syslog;

0 commit comments

Comments
 (0)