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>
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;
}