Skip to content
Snippets Groups Projects
Commit 4587c092 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

utils: always check length in findMapValue

parent fc962458
Branches
Tags
No related merge requests found
...@@ -173,7 +173,13 @@ unpackMsg(Blob b) { ...@@ -173,7 +173,13 @@ unpackMsg(Blob b) {
msgpack::unpacked unpackMsg(Blob b); msgpack::unpacked unpackMsg(Blob b);
msgpack::object* findMapValue(const msgpack::object& map, const char* key); msgpack::object* findMapValue(const msgpack::object& map, const char* key, size_t length);
msgpack::object* findMapValue(const msgpack::object& map, const std::string& key);
inline msgpack::object* findMapValue(const msgpack::object& map, const char* key) {
return findMapValue(map, key, strlen(key));
}
inline msgpack::object* findMapValue(const msgpack::object& map, const std::string& key) {
return findMapValue(map, key.c_str(), key.size());
}
} // namespace dht } // namespace dht
...@@ -277,28 +277,16 @@ unpackMsg(Blob b) { ...@@ -277,28 +277,16 @@ unpackMsg(Blob b) {
} }
msgpack::object* msgpack::object*
findMapValue(const msgpack::object& map, const char* key) { findMapValue(const msgpack::object& map, const char* key, size_t key_length) {
if (map.type != msgpack::type::MAP) throw msgpack::type_error(); if (map.type != msgpack::type::MAP) throw msgpack::type_error();
for (unsigned i = 0; i < map.via.map.size; i++) { for (unsigned i = 0; i < map.via.map.size; i++) {
auto& o = map.via.map.ptr[i]; auto& o = map.via.map.ptr[i];
if (o.key.type == msgpack::type::STR if (o.key.type == msgpack::type::STR
&& key_length == o.key.via.str.size
&& std::strncmp(o.key.via.str.ptr, key, o.key.via.str.size) == 0) && std::strncmp(o.key.via.str.ptr, key, o.key.via.str.size) == 0)
return &o.val; return &o.val;
} }
return nullptr; return nullptr;
} }
msgpack::object*
findMapValue(const msgpack::object& map, const std::string& key) {
if (map.type != msgpack::type::MAP) throw msgpack::type_error();
for (unsigned i = 0; i < map.via.map.size; i++) {
auto& o = map.via.map.ptr[i];
if (o.key.type == msgpack::type::STR
&& key.size() == o.key.via.str.size
&& std::strncmp(o.key.via.str.ptr, key.data(), o.key.via.str.size) == 0)
return &o.val;
}
return nullptr;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment