Skip to content

Commit 5d2d68c

Browse files
lavenzgfacebook-github-bot
authored andcommitted
Fix int truncation in JSI's default Runtime::push
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] Differential Revision: D132346566
1 parent c1acb2b commit 5d2d68c

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

‎packages/react-native/ReactCommon/jsi/jsi/jsi.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,13 @@ void Runtime::deleteProperty(const Object& object, const Value& name) {
460460
}
461461

462462
size_t Runtime::push(const Array& arr, const Value* elements, size_t count) {
463-
size_t newSize = size(arr);
463+
uint64_t newSize = size(arr);
464464
for (size_t i = 0; i < count; i++) {
465-
arr.setProperty(*this, Value((int)newSize), elements[i]);
465+
arr.setProperty(*this, Value(static_cast<double>(newSize)), elements[i]);
466466
++newSize;
467467
}
468-
arr.setProperty(*this, "length", Value((int)newSize));
469-
return newSize;
468+
arr.setProperty(*this, "length", Value(static_cast<double>(newSize)));
469+
return static_cast<size_t>(newSize);
470470
}
471471

472472
void Runtime::setRuntimeDataImpl(

‎packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,6 +2557,22 @@ TEST_P(JSITest, ArrayPush) {
25572557
EXPECT_EQ(arr.getValueAtIndex(rd, 1).getNumber(), 2);
25582558
EXPECT_EQ(arr.getValueAtIndex(rd, 2).getNumber(), 3);
25592559
EXPECT_TRUE(arr.getValueAtIndex(rd, 3).isUndefined());
2560+
2561+
// Push to an array whose length doesn't fit in an int
2562+
arr =
2563+
eval("var a = []; a.length = 2147483648; a;").getObject(rd).getArray(rd);
2564+
EXPECT_NO_THROW(newLength = arr.push(rd, 1));
2565+
EXPECT_EQ(newLength, 2147483649);
2566+
EXPECT_EQ(arr.length(rd), 2147483649);
2567+
EXPECT_TRUE(Value::strictEquals(rd, arr.getProperty(rd, "2147483648"), 1));
2568+
2569+
// Push past the maximum array length. As in JS, the element is stored under
2570+
// the non-index key "4294967295", then setting 'length' throws a RangeError.
2571+
arr =
2572+
eval("var a = []; a.length = 4294967295; a;").getObject(rd).getArray(rd);
2573+
EXPECT_THROW(arr.push(rd, 1), JSError);
2574+
EXPECT_EQ(arr.length(rd), 4294967295);
2575+
EXPECT_TRUE(Value::strictEquals(rd, arr.getProperty(rd, "4294967295"), 1));
25602576
}
25612577

25622578
TEST_P(JSITest, UInt8ArrayTest) {

0 commit comments

Comments
 (0)