Skip to content
Open
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
30 changes: 30 additions & 0 deletions include/msgpack/v2/x3_unpack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "msgpack/x3_unpack_decl.hpp"
#include "msgpack/x3_parse.hpp"

#include <cstdint>
#include <iterator>
#include <type_traits>

namespace msgpack {

/// @cond
Expand All @@ -29,13 +33,39 @@ MSGPACK_API_VERSION_NAMESPACE(v2) {

namespace detail {

// Reference mode hands out pointers into the input, so it is only safe when
// the whole input is one contiguous block that the caller keeps alive.
// Buffering iterators such as boost::spirit::multi_pass reallocate their
// internal storage while parsing; for those (and any non-random-access
// iterator) the data is always copied.
template <typename Iterator>
inline bool is_contiguous_input(Iterator first, Iterator last, std::random_access_iterator_tag) {
return first == last ||
reinterpret_cast<std::uintptr_t>(&*(last - 1)) - reinterpret_cast<std::uintptr_t>(&*first)
== static_cast<std::uintptr_t>(last - first - 1);
}

template <typename Iterator>
inline bool is_contiguous_input(Iterator, Iterator, std::input_iterator_tag) {
return false;
}

inline bool never_reference(msgpack::type::object_type, std::size_t, void*) {
return false;
}

template <typename Iterator>
inline void
unpack_imp(Iterator&& begin, Iterator&& end,
msgpack::zone& result_zone, msgpack::object& result, bool& referenced,
unpack_reference_func f, void* user_data,
unpack_limit const& limit)
{
typedef typename std::decay<Iterator>::type iterator_type;
if (f && !is_contiguous_input(
begin, end, typename std::iterator_traits<iterator_type>::iterator_category())) {
f = never_reference;
}
create_object_visitor v(f, user_data, limit);
v.set_zone(result_zone);
referenced = false;
Expand Down
66 changes: 66 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 <iterator>
#include <string>

#include <boost/spirit/include/support_multi_pass.hpp>

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

// Reference mode (zero-copy) must only hand out pointers into the caller's
// own contiguous input. A buffering input iterator such as
// boost::spirit::multi_pass keeps its data in an internal buffer that is
// reallocated while parsing, so those pointers would dangle.
static bool reference_all(msgpack::type::object_type, std::size_t, void*) { return true; }

static std::string pack_strings(int n, std::size_t len) {
std::stringstream ss;
msgpack::packer<std::stringstream> pk(ss);
pk.pack_array(static_cast<uint32_t>(n));
for (int i = 0; i < n; ++i) {
std::string e(len, static_cast<char>('A' + i));
pk.pack_str(static_cast<uint32_t>(e.size()));
pk.pack_str_body(e.data(), static_cast<uint32_t>(e.size()));
}
return ss.str();
}

BOOST_AUTO_TEST_CASE(reference_multi_pass)
{
const int n = 8;
std::string src = pack_strings(n, 600);
std::istringstream iss(src);
iss.unsetf(std::ios::skipws);
typedef std::istreambuf_iterator<char> base_it;
boost::spirit::multi_pass<base_it> first{base_it(iss)};
boost::spirit::multi_pass<base_it> last{base_it()};

bool referenced = true;
auto oh = msgpack::unpack(first, last, referenced, reference_all, nullptr, msgpack::unpack_limit());
BOOST_CHECK(!referenced);
msgpack::object const& o = oh.get();
BOOST_REQUIRE_EQUAL(o.type, msgpack::type::ARRAY);
BOOST_REQUIRE_EQUAL(o.via.array.size, static_cast<uint32_t>(n));
for (int i = 0; i < n; ++i) {
BOOST_CHECK_EQUAL(o.via.array.ptr[i].as<std::string>(), std::string(600, static_cast<char>('A' + i)));
}
}

BOOST_AUTO_TEST_CASE(reference_contiguous_still_zero_copy)
{
std::string src = pack_strings(2, 600);
{
bool referenced = false;
char const* b = src.data();
char const* e = src.data() + src.size();
auto oh = msgpack::unpack(b, e, referenced, reference_all, nullptr, msgpack::unpack_limit());
BOOST_CHECK(referenced);
char const* p = oh.get().via.array.ptr[0].via.str.ptr;
BOOST_CHECK(p >= src.data() && p < src.data() + src.size());
}
{
bool referenced = false;
auto b = src.cbegin();
auto e = src.cend();
auto oh = msgpack::unpack(b, e, referenced, reference_all, nullptr, msgpack::unpack_limit());
BOOST_CHECK(referenced);
char const* p = oh.get().via.array.ptr[0].via.str.ptr;
BOOST_CHECK(p >= src.data() && p < src.data() + src.size());
}
}

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