Skip to content

Commit 34ccf4f

Browse files
javachemeta-codesync[bot]
authored andcommitted
Skip memcpy when source vector is empty in MapBufferBuilder::build (#57398)
Summary: Pull Request resolved: #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
1 parent 5bf3e38 commit 34ccf4f

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,19 @@ MapBuffer MapBufferBuilder::build() {
183183

184184
std::vector<uint8_t> buffer(bufferSize);
185185
memcpy(buffer.data(), &header_, headerSize);
186-
memcpy(buffer.data() + headerSize, buckets_.data(), bucketSize);
187-
memcpy(
188-
buffer.data() + headerSize + bucketSize,
189-
dynamicData_.data(),
190-
dynamicData_.size());
186+
// buckets_.data() / dynamicData_.data() return nullptr when the vector is
187+
// empty; passing nullptr to memcpy is UB even with size 0 (glibc marks the
188+
// src argument nonnull) and trips UBSan halt-on-error on empty / scalar-only
189+
// MapBuffers.
190+
if (!buckets_.empty()) {
191+
memcpy(buffer.data() + headerSize, buckets_.data(), bucketSize);
192+
}
193+
if (!dynamicData_.empty()) {
194+
memcpy(
195+
buffer.data() + headerSize + bucketSize,
196+
dynamicData_.data(),
197+
dynamicData_.size());
198+
}
191199

192200
return MapBuffer(std::move(buffer));
193201
}

0 commit comments

Comments
 (0)