utils: always check length in findMapValue

This commit is contained in:
Adrien Béraud committed 2020-05-19 19:50:20 -04:00
1 parent fc96245891
commit 4587c0925f
2 files changed
+10 -16

No files matched your search

+2 -14
View File
@@ -277,28 +277,16 @@ unpackMsg(Blob b) {
}
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();
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_length == o.key.via.str.size
&& std::strncmp(o.key.via.str.ptr, key, o.key.via.str.size) == 0)
return &o.val;
}
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;
}
}