Skip to content

Commit 31b9f10

Browse files
javachefacebook-github-bot
authored andcommitted
Fix int overflow in useRawPropsJsiValue (#53504)
Summary: Pull Request resolved: #53504 Casting directly from double to int loses precision. Instead, match the (accidental) behaviour of the folly version, which always access the value as an int64_t first. ``` double value = 4294967040 (int)value = 2147483647 (overflow) (int)(int64_t)value = -256 (signed version of 4294967040) ``` Changelog: [General][Fixed] Casting rawValue to int was incorrectly truncating Reviewed By: zeyap, sammy-SC Differential Revision: D81228983 fbshipit-source-id: d68d4e63d7c7bc9a9226592756a1e53666d58978
1 parent c05f39e commit 31b9f10

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

packages/react-native/ReactCommon/react/renderer/core/RawValue.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ class RawValue {
394394

395395
static int
396396
castValue(jsi::Runtime* /*runtime*/, const jsi::Value& value, int* /*type*/) {
397-
double number = value.asNumber();
397+
// Casting directly from double to int loses precision, go via int64
398+
auto number = static_cast<int64_t>(value.asNumber());
398399
return static_cast<int>(number);
399400
}
400401

packages/react-native/ReactCommon/react/renderer/core/tests/RawPropsTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
#include <react/debug/flags.h>
1313
#include <react/renderer/core/ConcreteShadowNode.h>
1414
#include <react/renderer/core/PropsParserContext.h>
15+
#include <react/renderer/core/RawProps.h>
16+
#include <react/renderer/core/RawPropsParser.h>
1517
#include <react/renderer/core/ShadowNode.h>
1618
#include <react/renderer/core/propsConversions.h>
1719

18-
#include "TestComponent.h"
19-
2020
using namespace facebook;
2121
using namespace facebook::react;
2222

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include <memory>
9+
10+
#include <gtest/gtest.h>
11+
#include <hermes/hermes.h>
12+
#include <react/renderer/core/RawValue.h>
13+
14+
using namespace facebook;
15+
using namespace facebook::react;
16+
17+
TEST(RawValueTest, intValueDoesNotOverflow) {
18+
auto runtime = facebook::hermes::makeHermesRuntime();
19+
auto rawValue = RawValue(*runtime, jsi::Value(*runtime, 4294967040.0));
20+
EXPECT_EQ((int64_t)rawValue, 4294967040);
21+
EXPECT_EQ((int)rawValue, static_cast<int>(4294967040));
22+
23+
rawValue = RawValue(folly::dynamic(4294967040.0));
24+
EXPECT_EQ((int64_t)rawValue, 4294967040);
25+
EXPECT_EQ((int)rawValue, static_cast<int>(4294967040));
26+
}

0 commit comments

Comments
 (0)