From 31343afe84e03fa543a5ae73334eb583060cc46d Mon Sep 17 00:00:00 2001 From: Gang Zhao Date: Fri, 20 Nov 2026 09:30:14 -0800 Subject: [PATCH] Fix int truncation in JSI's default Runtime::push (#60062) Summary: JSI's default `Runtime::push`, used by runtimes that don't override it such as HermesSandboxRuntime, converted the element indices and the new length to `int`. Once an array's length passes INT_MAX, the elements go to the wrong keys and `length` gets a wrapped value: from length 2^31, `push(1)` throws "Invalid array length"; from length 2^32-1, it silently sets `length` to 0, emptying the array. Fix: convert to `double`, which holds every array index and length exactly, and keep the running length in a `uint64_t` so it can't wrap on 32-bit hosts, where `size_t` is 32 bits. A successful `length` store bounds the result by 2^32-1, so narrowing it to `size_t` on return is safe. Changelog: [Internal] Reviewed By: avp Differential Revision: D132346566 --- .../react-native/ReactCommon/jsi/jsi/jsi.cpp | 8 ++++---- .../ReactCommon/jsi/jsi/test/testlib.cpp | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp b/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp index 521001a8980d..56f59cb319f5 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp +++ b/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp @@ -460,13 +460,13 @@ void Runtime::deleteProperty(const Object& object, const Value& name) { } size_t Runtime::push(const Array& arr, const Value* elements, size_t count) { - size_t newSize = size(arr); + uint64_t newSize = size(arr); for (size_t i = 0; i < count; i++) { - arr.setProperty(*this, Value((int)newSize), elements[i]); + arr.setProperty(*this, Value(static_cast(newSize)), elements[i]); ++newSize; } - arr.setProperty(*this, "length", Value((int)newSize)); - return newSize; + arr.setProperty(*this, "length", Value(static_cast(newSize))); + return static_cast(newSize); } void Runtime::setRuntimeDataImpl( diff --git a/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp b/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp index 37b71538b87e..a8a24376e5d9 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp +++ b/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp @@ -2557,6 +2557,22 @@ TEST_P(JSITest, ArrayPush) { EXPECT_EQ(arr.getValueAtIndex(rd, 1).getNumber(), 2); EXPECT_EQ(arr.getValueAtIndex(rd, 2).getNumber(), 3); EXPECT_TRUE(arr.getValueAtIndex(rd, 3).isUndefined()); + + // Push to an array whose length doesn't fit in an int + arr = + eval("var a = []; a.length = 2147483648; a;").getObject(rd).getArray(rd); + EXPECT_NO_THROW(newLength = arr.push(rd, 1)); + EXPECT_EQ(newLength, 2147483649); + EXPECT_EQ(arr.length(rd), 2147483649); + EXPECT_TRUE(Value::strictEquals(rd, arr.getProperty(rd, "2147483648"), 1)); + + // Push past the maximum array length. As in JS, the element is stored under + // the non-index key "4294967295", then setting 'length' throws a RangeError. + arr = + eval("var a = []; a.length = 4294967295; a;").getObject(rd).getArray(rd); + EXPECT_THROW(arr.push(rd, 1), JSError); + EXPECT_EQ(arr.length(rd), 4294967295); + EXPECT_TRUE(Value::strictEquals(rd, arr.getProperty(rd, "4294967295"), 1)); } TEST_P(JSITest, UInt8ArrayTest) {