Repository navigation
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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:
|
The macOS build compiles tests with -Werror and -Wconversion, and pack_array(n) with an int n is a sign conversion there.
Contributor
|
Thanks, this fixes #1225. I reproduced Two requests before merging:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 returntrue, storingstr/bin/extpointers directly into the input instead of copying. For a buffering iterator such asboost::spirit::multi_pass, the backing storage is an internalstd::vector<char>that reallocates during parsing. Pointers captured for earlier values become dangling beforeunpackreturns, 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 v3unpackoverloads), 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, andreferencedis set tofalse. Zero-copy behavior overconst char*,std::string, andstd::vectoriterators 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 amulti_passwrapping anistreambuf_iteratorwith a reference function that always returnstrue, verifying thatreferencedisfalseand every element reads back intact.reference_contiguous_still_zero_copy: verifies that for aconst char*range andstd::string::const_iterator,referencedistrueand 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):reference_multi_passfails withAddressSanitizer: heap-use-after-free(amemcpyread from the freed buffer) andcheck !referenced has failed.reference_contiguous_still_zero_copypasses.msgpack_x3_parsepass (the 64 existing cases pass both before and after). Also compiles and passes under-std=c++14and-std=c++20with-Wall -Wextraand no warnings.Out of scope here:
std::dequeandstd::listinputs are handled only for reference mode (falling back to copy). Readingstr/bin/extfrom 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.