]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw/common: rgw_bl_str(bl) avoids bl.c_str() 55148/head
authorCasey Bodley <cbodley@redhat.com>
Fri, 12 Jan 2024 16:19:11 +0000 (11:19 -0500)
committerCasey Bodley <cbodley@redhat.com>
Fri, 12 Jan 2024 16:26:22 +0000 (11:26 -0500)
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 <cbodley@redhat.com>
src/rgw/rgw_common.h

index 2cf3d77f9e48354c7b7b05efc8a1db4862439327..ef9aa5420558285ac61fc0a1040728b6b3228d04 100644 (file)
@@ -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;
 }