From ceedd4195f8d5b3084f7b494ca0ab7965634246d Mon Sep 17 00:00:00 2001 From: xiangjinwu <17769960+xiangjinwu@users.noreply.github.com> Date: Tue, 2 Jan 2024 13:07:40 +0800 Subject: [PATCH] fix(pgwire): `TruncatedFmt` should respect utf8 char boundary (#14292) --- src/utils/pgwire/src/lib.rs | 1 + src/utils/pgwire/src/pg_protocol.rs | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/utils/pgwire/src/lib.rs b/src/utils/pgwire/src/lib.rs index d4018373db005..96d439b8b94ff 100644 --- a/src/utils/pgwire/src/lib.rs +++ b/src/utils/pgwire/src/lib.rs @@ -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; diff --git a/src/utils/pgwire/src/pg_protocol.rs b/src/utils/pgwire/src/pg_protocol.rs index 98ad88ffcce25..ebffb24d81118 100644 --- a/src/utils/pgwire/src/pg_protocol.rs +++ b/src/utils/pgwire/src/pg_protocol.rs @@ -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 { @@ -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)", + ); + } + } }