Skip to content

Commit

Permalink
fix: off-by-one in syslog.rs
Browse files Browse the repository at this point in the history
Fixes: #809
  • Loading branch information
CertainLach committed Dec 4, 2023
1 parent 5b25cb0 commit 6c6b70c
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/log/syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,9 @@ impl Write for SysLogWriter {
mid -= 1;
}

// index of last whitespace before byte cutoff
let ascii_utf8_len = 1;
mid = message[..mid]
.rfind(|c: char| c.is_ascii_whitespace())
.unwrap_or(mid)
+ ascii_utf8_len;
.unwrap_or(mid);

let left = &message[..mid];
let right = &message[mid..];
Expand Down Expand Up @@ -132,4 +129,15 @@ mod tests {

logger.log(&record);
}

#[test]
fn can_truncate_syslog_with_no_spaces() {
let logger = Syslog;
let record = log::Record::builder()
.args(format_args!("iwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercases"))
.level(log::Level::Info)
.build();

logger.log(&record);
}
}

0 comments on commit 6c6b70c

Please sign in to comment.