Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 45 additions & 15 deletions include/msgpack/v2/x3_parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include "msgpack/unpack_exception.hpp"

#include <limits>
#include <iterator>
#include <cstdint>
#include <vector>

#if __GNUC__ >= 4
#pragma GCC diagnostic push
Expand Down Expand Up @@ -127,6 +130,33 @@ struct app_specific {
app_specific(Vis&& vis):vis(vis) {}
std::vector<index_size> index_sizes;
Visitor vis;

// The matched str/bin/ext range may live in non-contiguous storage
// (std::deque, std::list, ...), where &range.front() cannot be read as
// `size` contiguous bytes. Return a pointer into the input only if the
// range is contiguous; otherwise copy it into a buffer.
template <typename Range>
char const* contiguous(Range const& r) {
auto first = r.begin();
auto last = r.end();
if (is_contiguous(first, last, typename std::iterator_traits<decltype(first)>::iterator_category())) {
return &*first;
}
buf.assign(first, last);
return buf.data();
}
private:
template <typename It>
static bool is_contiguous(It first, It last, std::random_access_iterator_tag) {
// the range is not empty here
return reinterpret_cast<std::uintptr_t>(&*(last - 1)) - reinterpret_cast<std::uintptr_t>(&*first)
== static_cast<std::uintptr_t>(last - first - 1);
}
template <typename It>
static bool is_contiguous(It, It, std::input_iterator_tag) {
return false;
}
std::vector<char> buf;
};

template <typename Visitor>
Expand Down Expand Up @@ -344,7 +374,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& str = _attr(ctx);
auto size = static_cast<uint32_t>(std::distance(str.begin(), str.end()));
app_specific.vis.visit_str(size ? &str.front() : nullptr, size);
app_specific.vis.visit_str(size ? app_specific.contiguous(str) : nullptr, size);
}
)
]
Expand All @@ -368,7 +398,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& str = _attr(ctx);
auto size = static_cast<uint32_t>(std::distance(str.begin(), str.end()));
app_specific.vis.visit_str(size ? &str.front() : nullptr, size);
app_specific.vis.visit_str(size ? app_specific.contiguous(str) : nullptr, size);
}
)
]
Expand All @@ -392,7 +422,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& str = _attr(ctx);
auto size = static_cast<uint32_t>(std::distance(str.begin(), str.end()));
app_specific.vis.visit_str(size ? &str.front() : nullptr, size);
app_specific.vis.visit_str(size ? app_specific.contiguous(str) : nullptr, size);
}
)
]
Expand All @@ -416,7 +446,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& str = _attr(ctx);
auto size = static_cast<uint32_t>(std::distance(str.begin(), str.end()));
app_specific.vis.visit_str(size ? &str.front() : nullptr, size);
app_specific.vis.visit_str(size ? app_specific.contiguous(str) : nullptr, size);
}
)
]
Expand All @@ -440,7 +470,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& bin = _attr(ctx);
auto size = static_cast<uint32_t>(std::distance(bin.begin(), bin.end()));
app_specific.vis.visit_bin(size ? &bin.front() : nullptr, size);
app_specific.vis.visit_bin(size ? app_specific.contiguous(bin) : nullptr, size);
}
)
]
Expand All @@ -464,7 +494,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& bin = _attr(ctx);
auto size = static_cast<uint32_t>(std::distance(bin.begin(), bin.end()));
app_specific.vis.visit_bin(size ? &bin.front() : nullptr, size);
app_specific.vis.visit_bin(size ? app_specific.contiguous(bin) : nullptr, size);
}
)
]
Expand All @@ -488,7 +518,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& bin = _attr(ctx);
auto size = static_cast<uint32_t>(std::distance(bin.begin(), bin.end()));
app_specific.vis.visit_bin(size ? &bin.front() : nullptr, size);
app_specific.vis.visit_bin(size ? app_specific.contiguous(bin) : nullptr, size);
}
)
]
Expand Down Expand Up @@ -596,7 +626,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& ext = _attr(ctx);
auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
}
)
]
Expand All @@ -620,7 +650,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& ext = _attr(ctx);
auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
}
)
]
Expand All @@ -644,7 +674,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& ext = _attr(ctx);
auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
}
)
]
Expand All @@ -668,7 +698,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& ext = _attr(ctx);
auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
}
)
]
Expand All @@ -692,7 +722,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& ext = _attr(ctx);
auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
}
)
]
Expand All @@ -716,7 +746,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& ext = _attr(ctx);
auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
}
)
]
Expand All @@ -740,7 +770,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& ext = _attr(ctx);
auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
}
)
]
Expand Down Expand Up @@ -768,7 +798,7 @@ const auto mp_object_def =
auto& app_specific = x3::get<tag_app_specific>(ctx).get();
auto const& ext = _attr(ctx);
auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
}
)
];
Expand Down
63 changes: 63 additions & 0 deletions test/msgpack_x3_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <sstream>
#include <limits>
#include <cmath>
#include <deque>
#include <list>
#include <vector>
#include <algorithm>

#define BOOST_TEST_MODULE MSGPACK_X3_PARSE
#include <boost/test/unit_test.hpp>
Expand Down Expand Up @@ -844,4 +848,63 @@ BOOST_AUTO_TEST_CASE(unpack_zone_3)
BOOST_CHECK_EQUAL(v, obj.as<std::string>());
}

// The matched str/bin/ext range must be read through the iterators.
// std::deque and std::list are not contiguous, so the visitor must not
// read `size` bytes starting from the first element.
template <typename Container>
static Container to_container(std::string const& s) {
return Container(s.begin(), s.end());
}

template <typename Container>
static void check_non_contiguous_str() {
std::string v(5000, 'A');
for (std::size_t i = 0; i < v.size(); ++i) v[i] = static_cast<char>('A' + i % 26);
std::stringstream ss;
msgpack::pack(ss, v);
Container c = to_container<Container>(ss.str());

auto oh = msgpack::unpack(c.begin(), c.end());
BOOST_CHECK_EQUAL(v, oh.get().template as<std::string>());
}

template <typename Container>
static void check_non_contiguous_bin() {
std::vector<char> v(5000);
for (std::size_t i = 0; i < v.size(); ++i) v[i] = static_cast<char>(i % 251);
std::stringstream ss;
msgpack::packer<std::stringstream> pk(ss);
pk.pack_bin(static_cast<uint32_t>(v.size()));
pk.pack_bin_body(v.data(), static_cast<uint32_t>(v.size()));
Container c = to_container<Container>(ss.str());

auto oh = msgpack::unpack(c.begin(), c.end());
BOOST_CHECK(oh.get().template as<std::vector<char> >() == v);
}

template <typename Container>
static void check_non_contiguous_ext() {
std::vector<char> v(5000);
for (std::size_t i = 0; i < v.size(); ++i) v[i] = static_cast<char>(i % 251);
std::stringstream ss;
msgpack::packer<std::stringstream> pk(ss);
pk.pack_ext(v.size(), 7);
pk.pack_ext_body(v.data(), static_cast<uint32_t>(v.size()));
Container c = to_container<Container>(ss.str());

auto oh = msgpack::unpack(c.begin(), c.end());
msgpack::object const& o = oh.get();
BOOST_REQUIRE_EQUAL(o.type, msgpack::type::EXT);
BOOST_CHECK_EQUAL(o.via.ext.type(), 7);
BOOST_REQUIRE_EQUAL(o.via.ext.size, v.size());
BOOST_CHECK(std::equal(v.begin(), v.end(), o.via.ext.data()));
}

BOOST_AUTO_TEST_CASE(non_contiguous_deque_str) { check_non_contiguous_str<std::deque<char> >(); }
BOOST_AUTO_TEST_CASE(non_contiguous_list_str) { check_non_contiguous_str<std::list<char> >(); }
BOOST_AUTO_TEST_CASE(non_contiguous_deque_bin) { check_non_contiguous_bin<std::deque<char> >(); }
BOOST_AUTO_TEST_CASE(non_contiguous_list_bin) { check_non_contiguous_bin<std::list<char> >(); }
BOOST_AUTO_TEST_CASE(non_contiguous_deque_ext) { check_non_contiguous_ext<std::deque<char> >(); }
BOOST_AUTO_TEST_CASE(non_contiguous_list_ext) { check_non_contiguous_ext<std::list<char> >(); }

#endif // defined(MSGPACK_USE_X3_PARSE) && MSGPACK_DEFAULT_API_VERSION >= 2
Loading