Skip memcpy when source vector is empty in MapBufferBuilder::build (#57398)

Summary:
Pull Request resolved: https://github.com/react/react-native/pull/57398

`buckets_.data()` and `dynamicData_.data()` return `nullptr` when the vector is empty, and glibc marks `memcpy`'s src argument as `nonnull`. Passing `nullptr` is UB even with a size of 0, which trips UBSan halt-on-error on any MapBuffer that has no buckets (`EMPTY()`) or no dynamic-data entries (scalar-only maps). Guard both memcpys with an empty check.

Changelog:
[General][Fixed] - Avoid `memcpy(_, nullptr, 0)` UB in `MapBufferBuilder::build` for empty / scalar-only MapBuffers

Reviewed By: cortinico

Differential Revision: D110316404

fbshipit-source-id: 2e58acf7ec59d222951c5846bcf88a369ad8ef87
This commit is contained in:
Pieter De Baets authored and meta-codesync[bot] committed 2026-07-01 08:45:25 -07:00
1 parent 5bf3e38db2
commit 34ccf4f331
1 file changed
+13 -5
@@ -183,11 +183,19 @@ MapBuffer MapBufferBuilder::build() {
std::vector<uint8_t> buffer(bufferSize);
memcpy(buffer.data(), &header_, headerSize);
memcpy(buffer.data() + headerSize, buckets_.data(), bucketSize);
memcpy(
buffer.data() + headerSize + bucketSize,
dynamicData_.data(),
dynamicData_.size());
// buckets_.data() / dynamicData_.data() return nullptr when the vector is
// empty; passing nullptr to memcpy is UB even with size 0 (glibc marks the
// src argument nonnull) and trips UBSan halt-on-error on empty / scalar-only
// MapBuffers.
if (!buckets_.empty()) {
memcpy(buffer.data() + headerSize, buckets_.data(), bucketSize);
}
if (!dynamicData_.empty()) {
memcpy(
buffer.data() + headerSize + bucketSize,
dynamicData_.data(),
dynamicData_.size());
}
return MapBuffer(std::move(buffer));
}