Skip to content

x3 parser: do not read str/bin/ext through a non-contiguous range - #1236

Merged
redboltz merged 1 commit into
msgpack:cpp_masterfrom
Perennos:fix-x3-noncontiguous-1224
Nov 27, 2026
Merged

redboltz merged 1 commit into
msgpack:cpp_masterfrom
Perennos:fix-x3-noncontiguous-1224

Conversation

@Perennos

Copy link
Copy Markdown

Fixes #1224

Under MSGPACK_USE_X3_PARSE, the str, bin, and ext actions in include/msgpack/v2/x3_parse.hpp pass &range.front() and the declared size to the visitor, and create_object_visitor then memcpys size bytes from that pointer. This is valid only when the input iterators point into contiguous storage. With std::deque or std::list iterators, it reads past the end of the first block or node and copies out-of-bounds bytes into the decoded object.

This change adds app_specific::contiguous() and uses it across all 15 str/bin/ext call sites:

  • For a contiguous random-access range, it returns &*first, keeping zero-copy and reference mode over std::string or std::vector unchanged.
  • Otherwise, it copies the range into a reusable buffer and returns a pointer to that buffer.

Tests
Added six cases in test/msgpack_x3_parse.cpp that unpack a 5000-byte str, bin, and ext from std::deque<char> and std::list<char> and compare the result with the input.

Built with GCC 13.5, Boost 1.74, -std=c++17 -fsanitize=address,undefined -DMSGPACK_USE_X3_PARSE:

  • Before the fix, each of the six new cases fails with AddressSanitizer: heap-buffer-overflow in create_object_visitor::visit_str (create_object_visitor.hpp:124) or the matching bin/ext path.
  • After the fix, all 70 cases in msgpack_x3_parse pass (the 64 existing cases pass both before and after). It also compiles under -std=c++14 and -std=c++20 with -Wall -Wextra and no warnings from x3_parse.hpp.

Out of scope
Reference mode over a non-contiguous or buffering iterator (#1225) is a separate lifetime problem and is not changed here.

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

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 msgpack#1224

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codecov-commenter

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 (77f7bf5).
⚠️ 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    #1236      +/-   ##
==============================================
- Coverage       85.36%   80.10%   -5.27%     
==============================================
  Files              80       80              
  Lines            5099     5851     +752     
  Branches          507      648     +141     
==============================================
+ Hits             4353     4687     +334     
- Misses            746     1164     +418     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@redboltz

Copy link
Copy Markdown
Contributor

Thanks, this fixes #1224 as described. I reproduced the six new cases failing with heap-buffer-overflow on current cpp_master and passing with this PR (clang, -std=c++17 -fsanitize=address,undefined, Boost 1.92); the existing 64 cases pass, and -std=c++20 -Wall -Wextra -Wconversion -Werror is clean.

One thing to be aware of: the O(1) endpoint check in is_contiguous can be fooled by a random-access non-contiguous container whose first and last blocks happen to be exactly size - 1 bytes apart while a middle block lives elsewhere. I reproduced this with a std::deque<char> using a custom allocator that hands out 512-byte blocks in the order 0, 3, 2: 512 of 1100 decoded bytes came from the wrong memory. It cannot happen with glibc malloc (chunk headers), so this is strictly an improvement over the current code and not a blocker, but could you (a) document the limitation in the comment, and (b) optionally use std::contiguous_iterator<It> for an exact answer when __cplusplus >= 202002L, falling back to the heuristic otherwise? That keeps std::string/std::vector zero-copy and makes std::deque always copy on C++20.

Note that #1237 touches the same lines of test/msgpack_x3_parse.cpp, so whichever lands second needs a rebase. I will merge this one first.

@redboltz
redboltz merged commit 812894a into msgpack:cpp_master Nov 27, 2026
46 of 48 checks passed
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