Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: logify could panic if truncating mid-code point #70

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions eventsource-client/src/event_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,9 @@ pub struct Event {
}

const LOGIFY_MAX_CHARS: usize = 100;
fn logify(bytes: &[u8]) -> &str {
fn logify(bytes: &[u8]) -> String {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the default logging level how often do we call this function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only call this method for trace and debug, so it shouldn't be executed at all with a default logging configuration, I wouldn't think.

Wanting to drop this truncation functionality entirely?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just checking that it wasn't all the time. I am down with this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think we should consider an alternative longer term. (dropping it and doing it in a logger or something.)

let stringified = from_utf8(bytes).unwrap_or("<bad utf8>");
if stringified.len() <= LOGIFY_MAX_CHARS {
stringified
} else {
&stringified[..LOGIFY_MAX_CHARS - 1]
}
stringified.chars().take(LOGIFY_MAX_CHARS).collect()
}

fn parse_field(line: &[u8]) -> Result<Option<(&str, &str)>> {
Expand Down Expand Up @@ -367,6 +363,8 @@ impl EventParser {

#[cfg(test)]
mod tests {
use std::str::FromStr;

use super::{Error::*, *};
use proptest::proptest;
use test_case::test_case;
Expand All @@ -388,6 +386,19 @@ mod tests {
}
}

#[test]
fn test_logify_handles_code_point_boundaries() {
let phase = String::from_str(
"这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如此。这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如此。这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如此。这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如此。",
)
.expect("Invalid sample string");

let input: &[u8] = phase.as_bytes();
let result = logify(input);

assert!(result == "这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如此。这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如此。这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如");
}

#[test]
fn test_parse_field_invalid() {
assert!(parse_field(b"").is_err());
Expand Down