Skip to content

Commit

Permalink
Handle UTF8 in native exception message (#41575)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #41575

We currently do not validate the incoming native exception message
before passing it to the char* constructor of TwineChar16.

Treat it as UTF-8 and convert it to UTF-16 before creating the
JavaScript exception.

Changelog: [Internal]

Reviewed By: tmikov

Differential Revision: D49551640

fbshipit-source-id: 762f8038b29818d804bda5a7f3b4762621c94336
  • Loading branch information
neildhar authored and facebook-github-bot committed Nov 29, 2023
1 parent 9e4fa20 commit 02b9447
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,25 @@ TEST_P(JSITest, NativeStateSymbolOverrides) {
42);
}

TEST_P(JSITest, UTF8ExceptionTest) {
// Test that a native exception containing UTF-8 characters is correctly
// passed through.
Function throwUtf8 = Function::createFromHostFunction(
rt,
PropNameID::forAscii(rt, "throwUtf8"),
1,
[](Runtime& rt, const Value&, const Value* args, size_t) -> Value {
throw JSINativeException(args[0].asString(rt).utf8(rt));
});
std::string utf8 = "👍";
try {
throwUtf8.call(rt, utf8);
FAIL();
} catch (const JSError& e) {
EXPECT_NE(e.getMessage().find(utf8), std::string::npos);
}
}

INSTANTIATE_TEST_CASE_P(
Runtimes,
JSITest,
Expand Down

0 comments on commit 02b9447

Please sign in to comment.