Repository navigation
Conversation
Contributor
|
Appveyor reported errors. |
mzfr
force-pushed
the
fix/empty-buffer-operations
branch
from
November 15, 2026 04:58
825e2fc to
995e854
Compare
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.
Upstream revision tested
The issues were reproduced against the latest
cpp_masterbranch before applying this patch:f5aa5a3782d57a2aeba64be2c2a329031657dd48825e2fc05dfeThe branch is based directly on that upstream commit. A review of all currently open issues and pull requests, plus targeted searches through closed reports, found no existing patch covering these cases.
Summary
This PR avoids zero-length
memcpy()andmemcmp()calls when an empty MessagePack value or buffer is represented by a null pointer.It fixes related cases in:
std::arraycharacter/byte adaptors;v4raw_refordering comparisons;sbuffer::write()afterrelease()or another operation leaves its data pointer null.The serialized values and public results are unchanged. The patch only skips memory operations when their length is zero.
Bugs
Null-backed empty STR and BIN objects
Valid empty objects can have a null data pointer. For example, unpacking an empty fixstr can produce:
The object copy visitor previously called
memcpy()unconditionally:The equality visitor similarly called
memcmp()for zero-length values:std::memcmp(m_ptr->via.str.ptr, v, size)Passing a null pointer to these functions is undefined behavior even when the length is zero. UBSan reports null-pointer arguments in both paths.
The copy and comparison operations are now skipped when
size == 0. STR and BIN type and size information, including the existing empty representation, remain unchanged.Empty object conversion through array adaptors
The same null-backed objects can be converted to:
char[N]andunsigned char[N];std::array<char, N>andstd::array<unsigned char, N>;std::byte[N].Those adaptors also passed the null object pointer to zero-length
memcpy()calls. Emptystd::array<char, 0>andstd::array<unsigned char, 0>values could additionally pass their nulldata()pointer while constructing an object with a zone.These adaptors now copy only when the object or container size is nonzero. Existing behavior is preserved, including null termination when an empty STR is converted to a C character array.
Empty
v4raw_reforderingv4raw_ref::operator==already avoidsmemcmp()for empty values, butoperator<andoperator>did not:Two default-constructed
v4raw_refobjects both have null pointers and zero size, so either ordering operation reachedmemcmp(nullptr, nullptr, 0).The ordering operators now follow the existing
raw_refbehavior: equal empty values are neither less than nor greater than each other, without callingmemcmp().Zero-length
sbufferwrites afterrelease()release()resets the internal state to:A subsequent write with a non-null source and zero length bypassed the existing null-source check. It then evaluated
m_data + m_sizeand called:UBSan reports a null destination pointer. The same state can occur for other empty or moved-from buffers.
sbuffer::write()now returns immediately whenlen == 0. Nonempty writes afterrelease()continue to allocate throughrealloc(nullptr, size)as before.Related prior fixes
sbuffer::write(), but a non-null source withlen == 0still reached the null destination afterrelease().v4raw_ref::operator==, but the<and>operators retained the same zero-lengthmemcmp()pattern.Testing
Before the patch, focused ASan/UBSan reproducers reported null-pointer arguments in:
object_equal_visitor;sbuffer::write();std::arrayadaptors;v4raw_refordering.After the patch: