Skip to content

Avoid zero-length operations on null buffers - #1216

Open
mzfr wants to merge 1 commit into
msgpack:cpp_masterfrom
mzfr:fix/empty-buffer-operations
Open

mzfr wants to merge 1 commit into
msgpack:cpp_masterfrom
mzfr:fix/empty-buffer-operations

Conversation

@mzfr

@mzfr mzfr commented Nov 14, 2026

Copy link
Copy Markdown

Upstream revision tested

The issues were reproduced against the latest cpp_master branch before applying this patch:

  • msgpack-cxx version: 9.0.0
  • Upstream commit: f5aa5a3782d57a2aeba64be2c2a329031657dd48
  • Patch commit: 825e2fc05dfe
    The 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() and memcmp() calls when an empty MessagePack value or buffer is represented by a null pointer.
It fixes related cases in:

  • STR and BIN object copying and equality;
  • C array and std::array character/byte adaptors;
  • empty v4raw_ref ordering comparisons;
  • sbuffer::write() after release() 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:

object.type = msgpack::type::STR;
object.via.str.ptr = nullptr;
object.via.str.size = 0;

The object copy visitor previously called memcpy() unconditionally:

char* ptr = static_cast<char*>(
    m_zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
m_ptr->via.str.ptr = ptr;
std::memcpy(ptr, v, size);

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] and unsigned char[N];
  • std::array<char, N> and std::array<unsigned char, N>;
  • std::byte[N].
    Those adaptors also passed the null object pointer to zero-length memcpy() calls. Empty std::array<char, 0> and std::array<unsigned char, 0> values could additionally pass their null data() 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_ref ordering

v4raw_ref::operator== already avoids memcmp() for empty values, but operator< and operator> did not:

if(size == x.size) {
    return std::memcmp(ptr, x.ptr, size) < 0;
}

Two default-constructed v4raw_ref objects both have null pointers and zero size, so either ordering operation reached memcmp(nullptr, nullptr, 0).
The ordering operators now follow the existing raw_ref behavior: equal empty values are neither less than nor greater than each other, without calling memcmp().

Zero-length sbuffer writes after release()

release() resets the internal state to:

m_size = 0;
m_data = nullptr;
m_alloc = 0;

A subsequent write with a non-null source and zero length bypassed the existing null-source check. It then evaluated m_data + m_size and called:

std::memcpy(m_data + m_size, buf, 0);

UBSan reports a null destination pointer. The same state can occur for other empty or moved-from buffers.
sbuffer::write() now returns immediately when len == 0. Nonempty writes after release() continue to allocate through realloc(nullptr, size) as before.

Related prior fixes

Testing

Before the patch, focused ASan/UBSan reproducers reported null-pointer arguments in:

  • object_equal_visitor;
  • the object-with-zone copy visitor;
  • sbuffer::write();
  • the affected C array and std::array adaptors;
  • v4raw_ref ordering.
    After the patch:
  • the full C++11 release suite passes: 39/39 tests;
  • the full C++17 release suite passes: 40/40 tests;
  • the full C++17 ASan/UBSan suite passes: 40/40 tests;
  • focused C++98 ASan/UBSan regression tests pass;
  • focused sanitizer tests pass with API versions 1, 2, and 3;
  • all original and extended standalone reproducer cases pass under ASan/UBSan.

@redboltz

Copy link
Copy Markdown
Contributor

Appveyor reported errors.
I've fixed appveyor environment. Could you rebase your PR ?

@mzfr
mzfr force-pushed the fix/empty-buffer-operations branch from 825e2fc to 995e854 Compare November 15, 2026 04:58

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.

2 participants