Skip to content

x3 unpack: do not reference non-contiguous input in reference mode - #1237

Open
Perennos wants to merge 2 commits into
msgpack:cpp_masterfrom
Perennos:fix-x3-reference-1225
Open

Perennos wants to merge 2 commits into
msgpack:cpp_masterfrom
Perennos:fix-x3-reference-1225

Conversation

@Perennos

Copy link
Copy Markdown

Fixes #1225. Related to #1236 (#1224), which touches different lines and is independent.

With MSGPACK_USE_X3_PARSE, msgpack::unpack(begin, end, f, ...) allows the reference function to return true, storing str/bin/ext pointers directly into the input instead of copying. For a buffering iterator such as boost::spirit::multi_pass, the backing storage is an internal std::vector<char> that reallocates during parsing. Pointers captured for earlier values become dangling before unpack returns, even though the caller keeps the source and iterators alive.

In detail::unpack_imp (include/msgpack/v2/x3_unpack.hpp, shared by the v2 and v3 unpack overloads), reference mode is now used only when the iterators are random-access and span a single contiguous block, verified by &*(last - 1) - &*first == last - first - 1. Otherwise, the reference function is ignored, data is copied into the zone as in copy mode, and referenced is set to false. Zero-copy behavior over const char*, std::string, and std::vector iterators is unchanged.

Tests added in test/msgpack_x3_parse.cpp (2 new cases):

  • reference_multi_pass: unpacks an array of eight 600-byte strings from a multi_pass wrapping an istreambuf_iterator with a reference function that always returns true, verifying that referenced is false and every element reads back intact.
  • reference_contiguous_still_zero_copy: verifies that for a const char* range and std::string::const_iterator, referenced is true and the resulting pointers lie within the source buffer.

Verification (GCC 13.5, Boost 1.74, -std=c++17 -fsanitize=address,undefined -DMSGPACK_USE_X3_PARSE):

  • Before the fix: reference_multi_pass fails with AddressSanitizer: heap-use-after-free (a memcpy read from the freed buffer) and check !referenced has failed. reference_contiguous_still_zero_copy passes.
  • After the fix: all 66 test cases in msgpack_x3_parse pass (the 64 existing cases pass both before and after). Also compiles and passes under -std=c++14 and -std=c++20 with -Wall -Wextra and no warnings.

Out of scope here: std::deque and std::list inputs are handled only for reference mode (falling back to copy). Reading str/bin/ext from such iterators in copy mode is the out-of-bounds read in #1224, fixed in #1236.

AI-assisted: drafted with an AI coding assistant; reproduced and tested locally before submitting.

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 <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Nov 25, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.10%. Comparing base (39cc081) to head (102238b).
⚠️ Report is 62 commits behind head on cpp_master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@              Coverage Diff               @@
##           cpp_master    #1237      +/-   ##
==============================================
- Coverage       85.36%   80.10%   -5.27%     
==============================================
  Files              80       80              
  Lines            5099     5850     +751     
  Branches          507      646     +139     
==============================================
+ Hits             4353     4686     +333     
- Misses            746     1164     +418     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The macOS build compiles tests with -Werror and -Wconversion, and
pack_array(n) with an int n is a sign conversion there.
@redboltz

Copy link
Copy Markdown
Contributor

Thanks, this fixes #1225. I reproduced reference_multi_pass failing with heap-use-after-free on current cpp_master and passing with this PR, with all 66 cases green under ASan/UBSan; reference_contiguous_still_zero_copy confirms std::string and const char* input stays zero-copy.

Two requests before merging:

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants