Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when calling substring() on a string containing emoji. #23609

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion RNTester/js/TextExample.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,13 @@ class TextExample extends React.Component<{}> {
Works with other text styles
</Text>
</RNTesterBlock>
<RNTesterBlock title="Substring Emoji (should only see 'test')">
<Text>{'test馃檭'.substring(0, 5)}</Text>
</RNTesterBlock>
</RNTesterPage>
);
}
}

const styles = StyleSheet.create({
backgroundColorText: {
left: 5,
Expand Down
6 changes: 6 additions & 0 deletions RNTester/js/TextExample.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,12 @@ exports.examples = [
);
},
},
{
title: "Substring Emoji (should only see 'test')",
render: function() {
return <Text>{'test馃檭'.substring(0, 5)}</Text>;
},
},
{
title: 'Text metrics',
render: function() {
Expand Down
10 changes: 3 additions & 7 deletions ReactCommon/jsi/JSCRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,10 @@ class JSCRuntime : public jsi::Runtime {
// JSStringRef utilities
namespace {
std::string JSStringToSTLString(JSStringRef str) {
std::string result;
size_t maxBytes = JSStringGetMaximumUTF8CStringSize(str);
result.resize(maxBytes);
size_t bytesWritten = JSStringGetUTF8CString(str, &result[0], maxBytes);
// JSStringGetUTF8CString writes the null terminator, so we want to resize
// to `bytesWritten - 1` so that `result` has the correct length.
result.resize(bytesWritten - 1);
return result;
std::vector<char> buffer(maxBytes);
JSStringGetUTF8CString(str, buffer.data(), maxBytes);
return std::string(buffer.data());
}

JSStringRef getLengthString() {
Expand Down