Skip to content

Commit 8f358a2

Browse files
fromcelticparkfacebook-github-bot
authored andcommittedJan 11, 2018
Report module id as string and as double, in case of invalid values are passed to nativeRequire
Differential Revision: D6695769 fbshipit-source-id: b578b9d52ed711fb5a3e51717ac555fa8a232d7a
1 parent 702b7e8 commit 8f358a2

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed
 

‎ReactCommon/cxxreact/JSCUtils.cpp

+9-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "JSCUtils.h"
44

5+
#include <folly/Conv.h>
6+
57
namespace facebook {
68
namespace react {
79

@@ -17,28 +19,20 @@ std::pair<uint32_t, uint32_t> parseNativeRequireParameters(
1719
const JSGlobalContextRef& context,
1820
const JSValueRef arguments[],
1921
size_t argumentCount) {
20-
double moduleId = 0, bundleId = 0;
22+
uint32_t moduleId = 0, bundleId = 0;
2123

24+
// use "getNumber" & "folly::to" to throw explicitely in case of an overflow
25+
// error during conversion
2226
if (argumentCount == 1) {
23-
moduleId = Value(context, arguments[0]).asNumber();
27+
moduleId = folly::to<uint32_t>(Value(context, arguments[0]).getNumberOrThrow());
2428
} else if (argumentCount == 2) {
25-
moduleId = Value(context, arguments[0]).asNumber();
26-
bundleId = Value(context, arguments[1]).asNumber();
29+
moduleId = folly::to<uint32_t>(Value(context, arguments[0]).getNumberOrThrow());
30+
bundleId = folly::to<uint32_t>(Value(context, arguments[1]).getNumberOrThrow());
2731
} else {
2832
throw std::invalid_argument("Got wrong number of args");
2933
}
3034

31-
if (moduleId < 0) {
32-
throw std::invalid_argument(folly::to<std::string>("Received invalid module ID: ",
33-
Value(context, arguments[0]).toString().str()));
34-
}
35-
36-
if (bundleId < 0) {
37-
throw std::invalid_argument(folly::to<std::string>("Received invalid bundle ID: ",
38-
Value(context, arguments[1]).toString().str()));
39-
}
40-
41-
return std::make_pair(static_cast<uint32_t>(bundleId), static_cast<uint32_t>(moduleId));
35+
return std::make_pair(bundleId, moduleId);
4236
}
4337

4438
}

‎ReactCommon/jschelpers/Value.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@ Value Value::makeError(JSContextRef ctx, const char *error, const char *stack)
191191
}
192192
}
193193

194+
void Value::throwTypeException(const std::string &expectedType) const {
195+
std::string wat("TypeError: Expected ");
196+
wat += expectedType;
197+
wat += ", instead got '";
198+
wat += toString().str();
199+
wat += "'";
200+
throw JSException(wat.c_str());
201+
}
202+
194203
Object::operator Value() const {
195204
return Value(m_context, m_obj);
196205
}

‎ReactCommon/jschelpers/Value.h

+9
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ class Value : public noncopyable {
302302
}
303303
}
304304

305+
double getNumberOrThrow() const {
306+
if (!isNumber()) {
307+
throwTypeException("Number");
308+
}
309+
return JSC_JSValueToNumber(context(), m_value, nullptr);
310+
}
311+
305312
int32_t asInteger() const {
306313
return static_cast<int32_t>(asNumber());
307314
}
@@ -355,7 +362,9 @@ class Value : public noncopyable {
355362
JSContextRef m_context;
356363
JSValueRef m_value;
357364

365+
void throwTypeException(const std::string &expectedType) const;
358366
static JSValueRef fromDynamicInner(JSContextRef ctx, const folly::dynamic& obj);
367+
359368
};
360369

361370
} }

0 commit comments

Comments
 (0)
Please sign in to comment.