From d9c0dddc322ee95d70a8970f25a4ee3d45131954 Mon Sep 17 00:00:00 2001 From: Perennos <333385283+Perennos@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:27:37 +0000 Subject: [PATCH 1/2] x3 unpack: never reference non-contiguous input in reference mode With MSGPACK_USE_X3_PARSE, unpack(begin, end, f, ...) lets the reference function return true, which stores pointers into the input instead of copying. For buffering iterators such as boost::spirit::multi_pass the "input" is an internal buffer that is reallocated while parsing, so the stored pointers dangle (heap-use-after-free). Only allow reference mode when the iterators are random access and the whole range is one contiguous block. Otherwise ignore the reference function and copy into the zone; referenced is then reported as false. Co-Authored-By: Claude Sonnet 5.5 --- include/msgpack/v2/x3_unpack.hpp | 30 +++++++++++++++ test/msgpack_x3_parse.cpp | 66 ++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/include/msgpack/v2/x3_unpack.hpp b/include/msgpack/v2/x3_unpack.hpp index 54ca98ea8..020ec5a41 100644 --- a/include/msgpack/v2/x3_unpack.hpp +++ b/include/msgpack/v2/x3_unpack.hpp @@ -21,6 +21,10 @@ #include "msgpack/x3_unpack_decl.hpp" #include "msgpack/x3_parse.hpp" +#include +#include +#include + namespace msgpack { /// @cond @@ -29,6 +33,27 @@ 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 +inline bool is_contiguous_input(Iterator first, Iterator last, std::random_access_iterator_tag) { + return first == last || + reinterpret_cast(&*(last - 1)) - reinterpret_cast(&*first) + == static_cast(last - first - 1); +} + +template +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 inline void unpack_imp(Iterator&& begin, Iterator&& end, @@ -36,6 +61,11 @@ unpack_imp(Iterator&& begin, Iterator&& end, unpack_reference_func f, void* user_data, unpack_limit const& limit) { + typedef typename std::decay::type iterator_type; + if (f && !is_contiguous_input( + begin, end, typename std::iterator_traits::iterator_category())) { + f = never_reference; + } create_object_visitor v(f, user_data, limit); v.set_zone(result_zone); referenced = false; diff --git a/test/msgpack_x3_parse.cpp b/test/msgpack_x3_parse.cpp index a55a27ee7..5cd99d8cb 100644 --- a/test/msgpack_x3_parse.cpp +++ b/test/msgpack_x3_parse.cpp @@ -3,6 +3,10 @@ #include #include #include +#include +#include + +#include #define BOOST_TEST_MODULE MSGPACK_X3_PARSE #include @@ -844,4 +848,66 @@ BOOST_AUTO_TEST_CASE(unpack_zone_3) BOOST_CHECK_EQUAL(v, obj.as()); } +// 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 pk(ss); + pk.pack_array(n); + for (int i = 0; i < n; ++i) { + std::string e(len, static_cast('A' + i)); + pk.pack_str(static_cast(e.size())); + pk.pack_str_body(e.data(), static_cast(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 base_it; + boost::spirit::multi_pass first{base_it(iss)}; + boost::spirit::multi_pass 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(n)); + for (int i = 0; i < n; ++i) { + BOOST_CHECK_EQUAL(o.via.array.ptr[i].as(), std::string(600, static_cast('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 From 102238b80bff73e9d30338c300f6117bae4de61a Mon Sep 17 00:00:00 2001 From: Alexander Marzouca <333385283+Perennos@users.noreply.github.com> Date: Wed, 25 Nov 2026 18:42:51 -0500 Subject: [PATCH 2/2] test: pass pack_array a uint32_t in the x3 reference test The macOS build compiles tests with -Werror and -Wconversion, and pack_array(n) with an int n is a sign conversion there. --- test/msgpack_x3_parse.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/msgpack_x3_parse.cpp b/test/msgpack_x3_parse.cpp index 5cd99d8cb..8a115da0c 100644 --- a/test/msgpack_x3_parse.cpp +++ b/test/msgpack_x3_parse.cpp @@ -857,7 +857,7 @@ static bool reference_all(msgpack::type::object_type, std::size_t, void*) { retu static std::string pack_strings(int n, std::size_t len) { std::stringstream ss; msgpack::packer pk(ss); - pk.pack_array(n); + pk.pack_array(static_cast(n)); for (int i = 0; i < n; ++i) { std::string e(len, static_cast('A' + i)); pk.pack_str(static_cast(e.size()));