Added zero size check before calling std::vector<T>::front().
Added empty checking tests for all containers.
This commit is contained in:
Takatoshi Kondo
2016-02-26 15:08:18 +09:00
parent 549c28459e
commit 60d4b2833d
5 changed files with 432 additions and 21 deletions

View File

@@ -70,14 +70,16 @@ struct convert<type::assoc_vector<K, V, Compare, Alloc> > {
msgpack::object const& operator()(msgpack::object const& o, type::assoc_vector<K, V, Compare, Alloc>& v) const {
if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
v.resize(o.via.map.size);
msgpack::object_kv* p = o.via.map.ptr;
msgpack::object_kv* const pend = o.via.map.ptr + o.via.map.size;
std::pair<K, V>* it(&v.front());
for (; p < pend; ++p, ++it) {
p->key.convert(it->first);
p->val.convert(it->second);
if (o.via.map.size != 0) {
msgpack::object_kv* p = o.via.map.ptr;
msgpack::object_kv* const pend = o.via.map.ptr + o.via.map.size;
std::pair<K, V>* it(&v.front());
for (; p < pend; ++p, ++it) {
p->key.convert(it->first);
p->val.convert(it->second);
}
std::sort(v.begin(), v.end(), type::detail::pair_first_less<K, V, Compare, Alloc>());
}
std::sort(v.begin(), v.end(), type::detail::pair_first_less<K, V, Compare, Alloc>());
return o;
}
};

View File

@@ -31,11 +31,29 @@ struct convert<std::vector<char, Alloc> > {
switch (o.type) {
case msgpack::type::BIN:
v.resize(o.via.bin.size);
std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
if (o.via.bin.size != 0) {
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
}
break;
case msgpack::type::STR:
v.resize(o.via.str.size);
std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
if (o.via.str.size != 0) {
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
}
break;
default:
throw msgpack::type_error();
@@ -51,7 +69,9 @@ struct pack<std::vector<char, Alloc> > {
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::vector<char, Alloc>& v) const {
uint32_t size = checked_get_container_size(v.size());
o.pack_bin(size);
o.pack_bin_body(&v.front(), size);
if (size != 0) {
o.pack_bin_body(&v.front(), size);
}
return o;
}
@@ -62,7 +82,9 @@ struct object<std::vector<char, Alloc> > {
void operator()(msgpack::object& o, const std::vector<char, Alloc>& v) const {
uint32_t size = checked_get_container_size(v.size());
o.type = msgpack::type::BIN;
o.via.bin.ptr = &v.front();
if (size != 0) {
o.via.bin.ptr = &v.front();
}
o.via.bin.size = size;
}
};
@@ -72,10 +94,12 @@ struct object_with_zone<std::vector<char, Alloc> > {
void operator()(msgpack::object::with_zone& o, const std::vector<char, Alloc>& v) const {
uint32_t size = checked_get_container_size(v.size());
o.type = msgpack::type::BIN;
char* ptr = static_cast<char*>(o.zone.allocate_align(size));
o.via.bin.ptr = ptr;
o.via.bin.size = size;
std::memcpy(ptr, &v.front(), size);
if (size != 0) {
char* ptr = static_cast<char*>(o.zone.allocate_align(size));
o.via.bin.ptr = ptr;
std::memcpy(ptr, &v.front(), size);
}
}
};

View File

@@ -31,11 +31,29 @@ struct convert<std::vector<unsigned char, Alloc> > {
switch (o.type) {
case msgpack::type::BIN:
v.resize(o.via.bin.size);
std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
if (o.via.bin.size != 0) {
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
}
break;
case msgpack::type::STR:
v.resize(o.via.str.size);
std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
if (o.via.str.size != 0) {
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
}
break;
default:
throw msgpack::type_error();
@@ -51,7 +69,9 @@ struct pack<std::vector<unsigned char, Alloc> > {
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::vector<unsigned char, Alloc>& v) const {
uint32_t size = checked_get_container_size(v.size());
o.pack_bin(size);
o.pack_bin_body(reinterpret_cast<char const*>(&v.front()), size);
if (size != 0) {
o.pack_bin_body(reinterpret_cast<char const*>(&v.front()), size);
}
return o;
}
@@ -62,7 +82,9 @@ struct object<std::vector<unsigned char, Alloc> > {
void operator()(msgpack::object& o, const std::vector<unsigned char, Alloc>& v) const {
uint32_t size = checked_get_container_size(v.size());
o.type = msgpack::type::BIN;
o.via.bin.ptr = reinterpret_cast<char const*>(&v.front());
if (size != 0) {
o.via.bin.ptr = reinterpret_cast<char const*>(&v.front());
}
o.via.bin.size = size;
}
};
@@ -72,10 +94,12 @@ struct object_with_zone<std::vector<unsigned char, Alloc> > {
void operator()(msgpack::object::with_zone& o, const std::vector<unsigned char, Alloc>& v) const {
uint32_t size = checked_get_container_size(v.size());
o.type = msgpack::type::BIN;
char* ptr = static_cast<char*>(o.zone.allocate_align(size));
o.via.bin.ptr = ptr;
o.via.bin.size = size;
std::memcpy(ptr, &v.front(), size);
if (size != 0) {
char* ptr = static_cast<char*>(o.zone.allocate_align(size));
o.via.bin.ptr = ptr;
std::memcpy(ptr, &v.front(), size);
}
}
};