From 77f7bf5ea48964fa7661a74d6d8a2437256c637b Mon Sep 17 00:00:00 2001 From: Perennos <333385283+Perennos@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:24:57 +0000 Subject: [PATCH] x3 parser: do not read str/bin/ext through a non-contiguous range The x3 str/bin/ext actions passed &range.front() and the declared size to the visitor, which memcpy()s size bytes. For std::deque or std::list iterators this reads past the first block or node. Pass a pointer into the input only when the range is contiguous; otherwise copy it first. Fixes #1224 Co-Authored-By: Claude Sonnet 5.5 --- include/msgpack/v2/x3_parse.hpp | 60 +++++++++++++++++++++++-------- test/msgpack_x3_parse.cpp | 63 +++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 15 deletions(-) diff --git a/include/msgpack/v2/x3_parse.hpp b/include/msgpack/v2/x3_parse.hpp index 936a0ab99..5389c35ed 100644 --- a/include/msgpack/v2/x3_parse.hpp +++ b/include/msgpack/v2/x3_parse.hpp @@ -21,6 +21,9 @@ #include "msgpack/unpack_exception.hpp" #include +#include +#include +#include #if __GNUC__ >= 4 #pragma GCC diagnostic push @@ -127,6 +130,33 @@ struct app_specific { app_specific(Vis&& vis):vis(vis) {} std::vector 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 + char const* contiguous(Range const& r) { + auto first = r.begin(); + auto last = r.end(); + if (is_contiguous(first, last, typename std::iterator_traits::iterator_category())) { + return &*first; + } + buf.assign(first, last); + return buf.data(); + } +private: + template + static bool is_contiguous(It first, It last, std::random_access_iterator_tag) { + // the range is not empty here + return reinterpret_cast(&*(last - 1)) - reinterpret_cast(&*first) + == static_cast(last - first - 1); + } + template + static bool is_contiguous(It, It, std::input_iterator_tag) { + return false; + } + std::vector buf; }; template @@ -344,7 +374,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& str = _attr(ctx); auto size = static_cast(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); } ) ] @@ -368,7 +398,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& str = _attr(ctx); auto size = static_cast(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); } ) ] @@ -392,7 +422,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& str = _attr(ctx); auto size = static_cast(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); } ) ] @@ -416,7 +446,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& str = _attr(ctx); auto size = static_cast(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); } ) ] @@ -440,7 +470,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& bin = _attr(ctx); auto size = static_cast(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); } ) ] @@ -464,7 +494,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& bin = _attr(ctx); auto size = static_cast(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); } ) ] @@ -488,7 +518,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& bin = _attr(ctx); auto size = static_cast(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); } ) ] @@ -596,7 +626,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& ext = _attr(ctx); auto size = static_cast(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); } ) ] @@ -620,7 +650,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& ext = _attr(ctx); auto size = static_cast(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); } ) ] @@ -644,7 +674,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& ext = _attr(ctx); auto size = static_cast(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); } ) ] @@ -668,7 +698,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& ext = _attr(ctx); auto size = static_cast(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); } ) ] @@ -692,7 +722,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& ext = _attr(ctx); auto size = static_cast(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); } ) ] @@ -716,7 +746,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& ext = _attr(ctx); auto size = static_cast(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); } ) ] @@ -740,7 +770,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& ext = _attr(ctx); auto size = static_cast(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); } ) ] @@ -768,7 +798,7 @@ const auto mp_object_def = auto& app_specific = x3::get(ctx).get(); auto const& ext = _attr(ctx); auto size = static_cast(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); } ) ]; diff --git a/test/msgpack_x3_parse.cpp b/test/msgpack_x3_parse.cpp index a55a27ee7..629e778d5 100644 --- a/test/msgpack_x3_parse.cpp +++ b/test/msgpack_x3_parse.cpp @@ -3,6 +3,10 @@ #include #include #include +#include +#include +#include +#include #define BOOST_TEST_MODULE MSGPACK_X3_PARSE #include @@ -844,4 +848,63 @@ BOOST_AUTO_TEST_CASE(unpack_zone_3) BOOST_CHECK_EQUAL(v, obj.as()); } +// 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 +static Container to_container(std::string const& s) { + return Container(s.begin(), s.end()); +} + +template +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('A' + i % 26); + std::stringstream ss; + msgpack::pack(ss, v); + Container c = to_container(ss.str()); + + auto oh = msgpack::unpack(c.begin(), c.end()); + BOOST_CHECK_EQUAL(v, oh.get().template as()); +} + +template +static void check_non_contiguous_bin() { + std::vector v(5000); + for (std::size_t i = 0; i < v.size(); ++i) v[i] = static_cast(i % 251); + std::stringstream ss; + msgpack::packer pk(ss); + pk.pack_bin(static_cast(v.size())); + pk.pack_bin_body(v.data(), static_cast(v.size())); + Container c = to_container(ss.str()); + + auto oh = msgpack::unpack(c.begin(), c.end()); + BOOST_CHECK(oh.get().template as >() == v); +} + +template +static void check_non_contiguous_ext() { + std::vector v(5000); + for (std::size_t i = 0; i < v.size(); ++i) v[i] = static_cast(i % 251); + std::stringstream ss; + msgpack::packer pk(ss); + pk.pack_ext(v.size(), 7); + pk.pack_ext_body(v.data(), static_cast(v.size())); + Container c = to_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 >(); } +BOOST_AUTO_TEST_CASE(non_contiguous_list_str) { check_non_contiguous_str >(); } +BOOST_AUTO_TEST_CASE(non_contiguous_deque_bin) { check_non_contiguous_bin >(); } +BOOST_AUTO_TEST_CASE(non_contiguous_list_bin) { check_non_contiguous_bin >(); } +BOOST_AUTO_TEST_CASE(non_contiguous_deque_ext) { check_non_contiguous_ext >(); } +BOOST_AUTO_TEST_CASE(non_contiguous_list_ext) { check_non_contiguous_ext >(); } + #endif // defined(MSGPACK_USE_X3_PARSE) && MSGPACK_DEFAULT_API_VERSION >= 2