diff --git a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp index f0ced11264f8..3619294b9ad9 100644 --- a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp @@ -135,21 +135,26 @@ void MapBufferBuilder::putMapBuffer(MapBuffer::Key key, const MapBuffer& map) { void MapBufferBuilder::putMapBufferList( MapBuffer::Key key, const std::vector& mapBufferList) { - auto offset = static_cast(dynamicData_.size()); + const auto offset = dynamicData_.size(); + size_t payloadSize = 0; + for (const auto& mapBuffer : mapBufferList) { + payloadSize += INT_SIZE + mapBuffer.size(); + } + dynamicData_.resize(offset + payloadSize); - // The bucket stores [offset][byteLength] for the whole list region; within it - // each child stays framed as [int32 childSize][child bytes] so the children - // remain individually delimited. - for (const MapBuffer& mapBuffer : mapBufferList) { - auto mapBufferSize = static_cast(mapBuffer.size()); - auto pos = static_cast(dynamicData_.size()); - dynamicData_.resize(pos + INT_SIZE + mapBufferSize, 0); - memcpy(dynamicData_.data() + pos, &mapBufferSize, INT_SIZE); + // Each child stays framed as [int32 childSize][child bytes]. + auto position = offset; + for (const auto& mapBuffer : mapBufferList) { + const auto mapBufferSize = static_cast(mapBuffer.size()); + memcpy(dynamicData_.data() + position, &mapBufferSize, INT_SIZE); memcpy( - dynamicData_.data() + pos + INT_SIZE, mapBuffer.data(), mapBufferSize); + dynamicData_.data() + position + INT_SIZE, + mapBuffer.data(), + mapBufferSize); + position += INT_SIZE + mapBufferSize; } - auto totalSize = static_cast(dynamicData_.size()) - offset; + const auto totalSize = static_cast(payloadSize); uint64_t data = packOffsetAndLength(offset, totalSize); // Uses the dedicated MapBufferList type so the entry is self-describing and // distinguishable from a single Map. diff --git a/packages/react-native/ReactCommon/react/renderer/mapbuffer/tests/MapBufferTest.cpp b/packages/react-native/ReactCommon/react/renderer/mapbuffer/tests/MapBufferTest.cpp index 05c70d203f59..aba19b669562 100644 --- a/packages/react-native/ReactCommon/react/renderer/mapbuffer/tests/MapBufferTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mapbuffer/tests/MapBufferTest.cpp @@ -236,6 +236,28 @@ TEST(MapBufferTest, testMapListEntriesAtNonZeroOffset) { EXPECT_EQ(readList[0].getInt(1), 42); } +TEST(MapBufferTest, testMapListEntriesWithDifferentPayloadSizes) { + auto child = MapBufferBuilder(); + child.putString(3, "a child with dynamic data"); + + std::vector children; + children.push_back(MapBufferBuilder::EMPTY()); + children.push_back(child.build()); + + auto builder = MapBufferBuilder(); + builder.putString(0, "before"); + builder.putMapBufferList(1, children); + builder.putString(2, "after"); + auto map = builder.build(); + + EXPECT_EQ(map.getString(0), "before"); + auto decoded = map.getMapBufferList(1); + ASSERT_EQ(decoded.size(), 2); + EXPECT_EQ(decoded[0].count(), 0); + EXPECT_EQ(decoded[1].getString(3), "a child with dynamic data"); + EXPECT_EQ(map.getString(2), "after"); +} + TEST(MapBufferTest, testIntBufferEntries) { auto builder = MapBufferBuilder();