From 02b94476af0680dc3521116e2a71aba5fe5acade Mon Sep 17 00:00:00 2001 From: Neil Dhar Date: Tue, 28 Nov 2023 16:50:06 -0800 Subject: [PATCH] Handle UTF8 in native exception message (#41575) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/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 --- .../ReactCommon/jsi/jsi/test/testlib.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp b/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp index e24284112be..abb008e1642 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp +++ b/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp @@ -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,