Keep the object_storage key terminator inside the key buffer

find_raw_string now honours the raw_string size instead of scanning for a nul.
This commit is contained in:
xirvik
2026-09-19 04:13:12 +00:00
committed by Jari Sundell
parent 3652329e2d
commit d1a6942ea7
3 changed files with 19 additions and 12 deletions
+1 -4
View File
@@ -26,10 +26,7 @@ const size_t object_storage::key_size;
object_storage::iterator
object_storage::find_raw_string(const torrent::raw_string& key) {
fixed_key_type<64> k;
k.set_c_str(key.data());
return find(k);
return find(key_type::from_raw_string(key));
}
object_storage::iterator
+1 -8
View File
@@ -158,14 +158,7 @@ fixed_key_type<MaxSize>::set_data(const value_type* src_data, size_type src_size
template <size_t MaxSize> inline void
fixed_key_type<MaxSize>::set_c_str(const value_type* src_data) {
value_type* itr = m_data;
const value_type* last = m_data + max_size;
while (itr != last && *src_data != '\0')
*itr++ = *src_data++;
*itr = '\0';
m_size = std::distance(m_data, itr);
set_c_str(src_data, std::strlen(src_data));
}
template <size_t MaxSize> inline void
+17
View File
@@ -59,6 +59,23 @@ TestObjectStorage::test_validate_keys() {
// The over-long key must not have been stored as the empty key.
CPPUNIT_ASSERT(m_storage.find_raw_string(torrent::raw_string::from_c_str("")) == m_storage.end());
// An over-long key must not be truncated into a match either.
CPPUNIT_ASSERT(m_storage.find_raw_string(torrent::raw_string::from_string(key_max + 'k')) == m_storage.end());
// set_c_str puts the terminating nul at m_data[size()], so size() has to
// stay below max_size for that write to land inside the array.
rpc::object_storage::key_type key_long;
key_long.set_c_str(std::string(rpc::object_storage::key_size + 1, 'k').c_str());
CPPUNIT_ASSERT(key_long.size() < rpc::object_storage::key_size);
m_storage.clear();
m_storage.insert_str("test_5", torrent::Object("a"), rpc::object_storage::flag_string_type);
// insert rejects a key holding a nul-char, so a lookup must not match past one.
CPPUNIT_ASSERT(m_storage.find_raw_string(torrent::raw_string("test_5\0x", 8)) == m_storage.end());
CPPUNIT_ASSERT(m_storage.find_raw_string(torrent::raw_string::from_c_str("test_5")) != m_storage.end());
m_storage.clear();
}