Repository navigation
Conversation
|
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 5852 +753
Branches 507 649 +142
==============================================
+ Hits 4353 4688 +335
- Misses 746 1164 +418 🚀 New features to boost your workflow:
|
|
Thanks, this fixes #1225. I reproduced Two requests before merging:
|
The check that tells whether a matched str/bin/ext range can be read in place was a private member of app_specific. Make it a function of msgpack::detail, so that the unpacker can use the same one, and document what it can and cannot tell: before C++20 it is an O(1) test of the two end elements, which a random-access container whose first and last blocks happen to be size - 1 bytes apart while a block between them lives elsewhere can fool. With C++20 (std::contiguous_iterator is available) the answer is exact: only an iterator that models it is read in place, and anything else, std::deque included, is copied. std::string, std::vector and plain pointers stay zero-copy. Tests: the helper on a string, a vector, a pointer and a list, and, on C++20, a random-access iterator over three blocks laid out to fool the endpoint check, which must now be decoded through a copy. AI assistance: the change and its tests were drafted with an AI coding assistant.
Reference mode hands out pointers into the input, which is only safe when the whole input is one block the caller keeps alive. A buffering iterator such as boost::spirit::multi_pass reallocates its storage while parsing, so str, bin and ext objects pointed into freed memory (msgpack#1225). unpack_imp now asks the shared is_contiguous helper and, when the input is not contiguous, swaps the reference callback for one that always copies. std::string, std::vector and raw pointers stay zero-copy. AI assistance: the change and its tests were drafted with an AI coding assistant. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
102238b to
792744b
Compare
|
Thanks for the review. The branch is rebased onto Shared helper. C++20. When One behaviour change comes with the exact answer: on C++20 an iterator that is contiguous in memory but does not model Tests. What I ran (ubuntu 24.04, gcc 13.3 and clang 18.1,
The first commit alone also builds and passes on all four (71/71 and 72/72). With the old Not run: libc++ (my Boost.Test link failed in that setup, so I have no result), MSVC, and macOS. AI assistance: the change and its tests were drafted with an AI coding assistant; I reviewed and ran them as above. |
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.