Skip to content

Commit

Permalink
fix(pgwire): TruncatedFmt should respect utf8 char boundary (#14292)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangjinwu authored Jan 2, 2024
1 parent 144e87a commit ceedd41
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/utils/pgwire/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#![feature(trusted_len)]
#![feature(lazy_cell)]
#![feature(buf_read_has_data_left)]
#![feature(round_char_boundary)]
#![expect(clippy::doc_markdown, reason = "FIXME: later")]

pub mod error;
Expand Down
18 changes: 16 additions & 2 deletions src/utils/pgwire/src/pg_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,8 +1139,9 @@ pub mod truncated_fmt {
}

if self.remaining < s.len() {
self.f.write_str(&s[0..self.remaining])?;
self.remaining = 0;
let actual = s.floor_char_boundary(self.remaining);
self.f.write_str(&s[0..actual])?;
self.remaining -= actual;
self.f.write_str("...(truncated)")?;
self.finished = true; // so that ...(truncated) is printed exactly once
} else {
Expand Down Expand Up @@ -1180,4 +1181,17 @@ pub mod truncated_fmt {
.write_fmt(format_args!("{}", self.0))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_trunc_utf8() {
assert_eq!(
format!("{}", TruncatedFmt(&"select '🌊';", 10)),
"select '...(truncated)",
);
}
}
}

0 comments on commit ceedd41

Please sign in to comment.