Skip to content

Commit 43961b8

Browse files
committed
Fix the crash caused by UB in the C++ code
The result of casting a double to uint64_t is undefined, if the double is greater than the numeric limits of uint64_t.
1 parent a6dbc83 commit 43961b8

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

packages/webgpu/cpp/jsi/RNFJSIConverter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#include "Dispatcher.h"
2929
#include "ThreadPool.h"
3030

31+
// This number is the maximum integer that can be represented exactly as a double
32+
#define MAX_SAFE_INTEGER uint64_t(9007199254740991)
33+
3134
#if __has_include(<cxxabi.h>)
3235
#include <cxxabi.h>
3336
#endif
@@ -122,7 +125,7 @@ template <> struct JSIConverter<uint64_t> {
122125
static uint64_t fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) {
123126
if (arg.isNumber()) {
124127
double value = arg.asNumber();
125-
if (value < 0 || value > static_cast<double>(std::numeric_limits<uint64_t>::max())) {
128+
if (value < 0 || value > MAX_SAFE_INTEGER) {
126129
throw jsi::JSError(runtime, "Number out of range for uint64_t");
127130
}
128131
return static_cast<uint64_t>(value);
@@ -132,7 +135,7 @@ template <> struct JSIConverter<uint64_t> {
132135
}
133136

134137
static jsi::Value toJSI(jsi::Runtime& runtime, uint64_t arg) {
135-
if (arg <= static_cast<uint64_t>(std::numeric_limits<double>::max())) {
138+
if (arg <= MAX_SAFE_INTEGER) {
136139
return jsi::Value(static_cast<double>(arg));
137140
} else {
138141
throw jsi::JSError(runtime, "Number too large to be represented as a double");

0 commit comments

Comments
 (0)