From b111c0efacd049861914a4610a055ed49e599e83 Mon Sep 17 00:00:00 2001 From: Yaohui Chen Date: Mon, 20 Apr 2026 19:43:34 -0700 Subject: [PATCH] =?UTF-8?q?MapBufferBuilder:=20fix=20size=5Ft=E2=86=92int3?= =?UTF-8?q?2=20partial-write=20in=20putString/putMapBuffer=20length=20enco?= =?UTF-8?q?ding=20(#56525)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56525 The MapBuffer wire format encodes lengths and offsets as `int32_t` (`MapBuffer::getString` / `getMapBuffer` read them as `*reinterpret_cast(...)`). But in `putString` and `putMapBuffer`: ```cpp auto strSize = value.size(); // size_t — 8 bytes on 64-bit memcpy(dynamicData_.data() + offset, &strSize, INT_SIZE); // copies first 4 of 8 bytes ``` `auto` deduces `size_t` from `.size()`, and `memcpy(&size_t_var, ..., 4)` writes only the **first 4 bytes** of an 8-byte object: - **little-endian:** writes the low 4 bytes — silent truncation if size > UINT32_MAX - **big-endian:** writes the high 4 bytes — encodes **zero** for normal sizes, producing a corrupt buffer the reader then trusts `putMapBufferList` already had the correct pattern: `auto offset = static_cast(dynamicData_.size())`. This diff applies that same explicit `static_cast` to `putString` (`strSize`, `offset`) and `putMapBuffer` (`mapBufferSize`, `offset`) so the value being `memcpy`'d is exactly `INT_SIZE` bytes wide. Changelog: [Internal] Reviewed By: NickGerleman Differential Revision: D100376322 fbshipit-source-id: ad0d505555e057240bf376795e867c52125590d8 --- .../renderer/mapbuffer/MapBufferBuilder.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp index 3f0c476d391..eb6e859fec2 100644 --- a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp @@ -84,11 +84,16 @@ void MapBufferBuilder::putLong(MapBuffer::Key key, int64_t value) { } void MapBufferBuilder::putString(MapBuffer::Key key, const std::string& value) { - auto strSize = value.size(); + // The wire format encodes lengths and offsets as int32_t (see + // MapBuffer::getString). Without an explicit narrowing cast, `auto` deduces + // size_t (8 bytes on 64-bit) and `memcpy(&x, ..., INT_SIZE)` then copies only + // the first 4 bytes of an 8-byte value: silent truncation on little-endian, + // wrong (high) bytes on big-endian. + auto strSize = static_cast(value.size()); const char* strData = value.data(); // format [length of string (int)] + [Array of Characters in the string] - auto offset = dynamicData_.size(); + auto offset = static_cast(dynamicData_.size()); dynamicData_.resize(offset + INT_SIZE + strSize, 0); memcpy(dynamicData_.data() + offset, &strSize, INT_SIZE); memcpy(dynamicData_.data() + offset + INT_SIZE, strData, strSize); @@ -102,9 +107,12 @@ void MapBufferBuilder::putString(MapBuffer::Key key, const std::string& value) { } void MapBufferBuilder::putMapBuffer(MapBuffer::Key key, const MapBuffer& map) { - auto mapBufferSize = map.size(); + // Wire format encodes lengths and offsets as int32_t (see + // MapBuffer::getMapBuffer). Cast explicitly so memcpy(&x, ..., INT_SIZE) + // copies the full value, not the first 4 bytes of an 8-byte size_t. + auto mapBufferSize = static_cast(map.size()); - auto offset = dynamicData_.size(); + auto offset = static_cast(dynamicData_.size()); // format [length of buffer (int)] + [bytes of MapBuffer] dynamicData_.resize(offset + INT_SIZE + mapBufferSize, 0);