[DRAFT] Fix JSCRuntime::createStringFromUtf8 to support strings with embedded null chars #1930
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
DRAFT - Do not merge
Please select one of the following
Summary
The current implementation of
JSCRuntime::createStringFromUtf8
ignores the length parameter that it's given, and then blindly passes the char array toJSStringCreateWithUTF8CString
which assumes the char array is a null terminated string. This means we can't pass strings with embedded null chars through the JSI boundary from C++ to JS. Instead, we can internally convert the string from UTF-8 to UTF-16, and then callJSStringCreateWithCharacters
which allows us to pass an explicit length for our array of chars.JSStringCreateWithUTF8CString
internally has an optimization for strings that only contain ASCII characters, which we will miss out on by switching toJSStringCreateWithCharacters
. The JSC API surface isn't rich enough for us to avoid that, so if there's a perf impact here, then so be it. We will trade perf for correctness.This is based on PR 34300 upstream in the RN repo. I'm not assuming infinite flexibility in this version. We're only building for Apple platforms, so we know the endianness and the expected size of a
JSChar
.