Skip to content

Commit 39433e8

Browse files
committed
Merge pull request #437 from redboltz/bp_435
Backported #435 to version 1.4.0.
2 parents 66a5fcf + 1088aa5 commit 39433e8

File tree

5 files changed

+440
-21
lines changed

5 files changed

+440
-21
lines changed

include/msgpack/adaptor/map.hpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,16 @@ struct convert<type::assoc_vector<K, V, Compare, Alloc> > {
7272
msgpack::object const& operator()(msgpack::object const& o, type::assoc_vector<K, V, Compare, Alloc>& v) const {
7373
if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
7474
v.resize(o.via.map.size);
75-
msgpack::object_kv* p = o.via.map.ptr;
76-
msgpack::object_kv* const pend = o.via.map.ptr + o.via.map.size;
77-
std::pair<K, V>* it(&v.front());
78-
for (; p < pend; ++p, ++it) {
79-
p->key.convert(it->first);
80-
p->val.convert(it->second);
75+
if (o.via.map.size != 0) {
76+
msgpack::object_kv* p = o.via.map.ptr;
77+
msgpack::object_kv* const pend = o.via.map.ptr + o.via.map.size;
78+
std::pair<K, V>* it(&v.front());
79+
for (; p < pend; ++p, ++it) {
80+
p->key.convert(it->first);
81+
p->val.convert(it->second);
82+
}
83+
std::sort(v.begin(), v.end(), type::detail::pair_first_less<K, V, Compare, Alloc>());
8184
}
82-
std::sort(v.begin(), v.end(), type::detail::pair_first_less<K, V, Compare, Alloc>());
8385
return o;
8486
}
8587
};
@@ -194,14 +196,22 @@ struct object_with_zone<std::map<K, V, Compare, Alloc> > {
194196
}
195197
else {
196198
uint32_t size = checked_get_container_size(v.size());
199+
197200
msgpack::object_kv* p = static_cast<msgpack::object_kv*>(o.zone.allocate_align(sizeof(msgpack::object_kv)*size));
198201
msgpack::object_kv* const pend = p + size;
199202
o.via.map.ptr = p;
200203
o.via.map.size = size;
201204
typename std::map<K, V, Compare, Alloc>::const_iterator it(v.begin());
202205
do {
206+
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
207+
#pragma GCC diagnostic push
208+
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
209+
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
203210
p->key = msgpack::object(it->first, o.zone);
204211
p->val = msgpack::object(it->second, o.zone);
212+
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
213+
#pragma GCC diagnostic pop
214+
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
205215
++p;
206216
++it;
207217
} while(p < pend);

include/msgpack/adaptor/vector_char.hpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,29 @@ struct convert<std::vector<char, Alloc> > {
3030
switch (o.type) {
3131
case msgpack::type::BIN:
3232
v.resize(o.via.bin.size);
33-
std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
33+
if (o.via.bin.size != 0) {
34+
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
35+
#pragma GCC diagnostic push
36+
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
37+
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
38+
std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
39+
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
40+
#pragma GCC diagnostic pop
41+
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
42+
}
3443
break;
3544
case msgpack::type::STR:
3645
v.resize(o.via.str.size);
37-
std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
46+
if (o.via.str.size != 0) {
47+
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
48+
#pragma GCC diagnostic push
49+
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
50+
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
51+
std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
52+
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
53+
#pragma GCC diagnostic pop
54+
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
55+
}
3856
break;
3957
default:
4058
throw msgpack::type_error();
@@ -50,7 +68,9 @@ struct pack<std::vector<char, Alloc> > {
5068
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::vector<char, Alloc>& v) const {
5169
uint32_t size = checked_get_container_size(v.size());
5270
o.pack_bin(size);
53-
o.pack_bin_body(&v.front(), size);
71+
if (size != 0) {
72+
o.pack_bin_body(&v.front(), size);
73+
}
5474

5575
return o;
5676
}
@@ -61,7 +81,9 @@ struct object<std::vector<char, Alloc> > {
6181
void operator()(msgpack::object& o, const std::vector<char, Alloc>& v) const {
6282
uint32_t size = checked_get_container_size(v.size());
6383
o.type = msgpack::type::BIN;
64-
o.via.bin.ptr = &v.front();
84+
if (size != 0) {
85+
o.via.bin.ptr = &v.front();
86+
}
6587
o.via.bin.size = size;
6688
}
6789
};
@@ -71,10 +93,12 @@ struct object_with_zone<std::vector<char, Alloc> > {
7193
void operator()(msgpack::object::with_zone& o, const std::vector<char, Alloc>& v) const {
7294
uint32_t size = checked_get_container_size(v.size());
7395
o.type = msgpack::type::BIN;
74-
char* ptr = static_cast<char*>(o.zone.allocate_align(size));
75-
o.via.bin.ptr = ptr;
7696
o.via.bin.size = size;
77-
std::memcpy(ptr, &v.front(), size);
97+
if (size != 0) {
98+
char* ptr = static_cast<char*>(o.zone.allocate_align(size));
99+
o.via.bin.ptr = ptr;
100+
std::memcpy(ptr, &v.front(), size);
101+
}
78102
}
79103
};
80104

include/msgpack/adaptor/vector_unsigned_char.hpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,29 @@ struct convert<std::vector<unsigned char, Alloc> > {
3030
switch (o.type) {
3131
case msgpack::type::BIN:
3232
v.resize(o.via.bin.size);
33-
std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
33+
if (o.via.bin.size != 0) {
34+
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
35+
#pragma GCC diagnostic push
36+
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
37+
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
38+
std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
39+
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
40+
#pragma GCC diagnostic pop
41+
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
42+
}
3443
break;
3544
case msgpack::type::STR:
3645
v.resize(o.via.str.size);
37-
std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
46+
if (o.via.str.size != 0) {
47+
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
48+
#pragma GCC diagnostic push
49+
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
50+
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
51+
std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
52+
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
53+
#pragma GCC diagnostic pop
54+
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
55+
}
3856
break;
3957
default:
4058
throw msgpack::type_error();
@@ -50,7 +68,9 @@ struct pack<std::vector<unsigned char, Alloc> > {
5068
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::vector<unsigned char, Alloc>& v) const {
5169
uint32_t size = checked_get_container_size(v.size());
5270
o.pack_bin(size);
53-
o.pack_bin_body(reinterpret_cast<char const*>(&v.front()), size);
71+
if (size != 0) {
72+
o.pack_bin_body(reinterpret_cast<char const*>(&v.front()), size);
73+
}
5474

5575
return o;
5676
}
@@ -61,7 +81,9 @@ struct object<std::vector<unsigned char, Alloc> > {
6181
void operator()(msgpack::object& o, const std::vector<unsigned char, Alloc>& v) const {
6282
uint32_t size = checked_get_container_size(v.size());
6383
o.type = msgpack::type::BIN;
64-
o.via.bin.ptr = reinterpret_cast<char const*>(&v.front());
84+
if (size != 0) {
85+
o.via.bin.ptr = reinterpret_cast<char const*>(&v.front());
86+
}
6587
o.via.bin.size = size;
6688
}
6789
};
@@ -71,10 +93,12 @@ struct object_with_zone<std::vector<unsigned char, Alloc> > {
7193
void operator()(msgpack::object::with_zone& o, const std::vector<unsigned char, Alloc>& v) const {
7294
uint32_t size = checked_get_container_size(v.size());
7395
o.type = msgpack::type::BIN;
74-
char* ptr = static_cast<char*>(o.zone.allocate_align(size));
75-
o.via.bin.ptr = ptr;
7696
o.via.bin.size = size;
77-
std::memcpy(ptr, &v.front(), size);
97+
if (size != 0) {
98+
char* ptr = static_cast<char*>(o.zone.allocate_align(size));
99+
o.via.bin.ptr = ptr;
100+
std::memcpy(ptr, &v.front(), size);
101+
}
78102
}
79103
};
80104

0 commit comments

Comments
 (0)