From 26b361d1edbdf070724f0e0c5ba30accaf57d475 Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Fri, 12 Jan 2024 11:19:11 -0500 Subject: [PATCH] rgw/common: rgw_bl_str(bl) avoids bl.c_str() where bl contains multiple buffer segments, c_str() has to rellocate and copy those segments into a single buffer. use c_str() instead, which just copies each segment into the resulting string this allows the function to take the bufferlist argument by const ref Signed-off-by: Casey Bodley --- src/rgw/rgw_common.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/rgw/rgw_common.h b/src/rgw/rgw_common.h index 2cf3d77f9e483..ef9aa54205582 100644 --- a/src/rgw/rgw_common.h +++ b/src/rgw/rgw_common.h @@ -1824,13 +1824,14 @@ static inline ssize_t rgw_unescape_str(const std::string& s, ssize_t ofs, return std::string::npos; } -static inline std::string rgw_bl_str(ceph::buffer::list& raw) +/// Return a string copy of the given bufferlist with trailing nulls removed +static inline std::string rgw_bl_str(const ceph::buffer::list& bl) { - size_t len = raw.length(); - std::string s(raw.c_str(), len); - while (len && !s[len - 1]) { - --len; - s.resize(len); + // use to_str() instead of c_str() so we don't reallocate a flat bufferlist + std::string s = bl.to_str(); + // with to_str(), the result may include null characters. trim trailing nulls + while (!s.empty() && s.back() == '\0') { + s.pop_back(); } return s; } -- 2.39.5