Skip to content

Fix Text truncation at NULL character on iOS and Android (#24129) - #57906

Closed
kulkarni-rohan wants to merge 1 commit into
react:mainfrom
kulkarni-rohan:fix/24129-null-char
Closed

Fix Text truncation at NULL character on iOS and Android (#24129)#57906
kulkarni-rohan wants to merge 1 commit into
react:mainfrom
kulkarni-rohan:fix/24129-null-char

Conversation

@kulkarni-rohan

Copy link
Copy Markdown
Contributor

Fixes #24129

Summary:

Problem: <Text>{'Hello \u0000 World'}</Text> renders as "Hello" (truncated at \u0000). Does NOT reproduce when Debug JS Remotely (JSON preserves \u0000), but reproduces in production Hermes+Fabric on iOS and Android.

Root cause: JS String preserves \u0000 (length includes it), C++ std::string from JSI utf8() also preserves via size. Truncation happens at platform bridge where C-string NUL-terminated APIs are used:

  • iOS: RCTAttributedTextUtils.mm:420 stringWithUTF8String:fragment.string.c_str() stops at embedded \0
  • iOS: RCTConversions.h RCTNSStringFromString via stringWithCString: and reverse std::string{UTF8String}
  • iOS: RCTTurboModule.mm convertJSIStringToNSString same
  • Android: JavaTurboModule.cpp NewStringUTF(c_str()) expects NUL-terminated

Fix (4 files, ~10 lines, no new API): Replace C-string APIs with length-aware [[NSString alloc] initWithBytes:data length:size encoding:NSUTF8StringEncoding] (same as existing correct pattern in FollyConvert.mm:31 and MapBufferBuilder.cpp) and Android convertUTF8ToUTF16 + NewString(jchar*, len) + assign(jsChars, GetStringUTFLength()). All surfaces (Text, TextInput, accessibility, TurboModule params) fixed at once because shared converters are fixed.

Changelog:

[GENERAL] [FIXED] - Fix Text truncation when string contains NULL character \u0000 (#24129)

Test Plan:

Reproduction (RNTester):

  • Add screen Text > NullCharacter with {'Hello\u0000World'} and 'A\u0000B\u0000C'
  • Before: "Hello" truncated
  • After: "HelloWorld" full (invisible \0 zero-width but World visible, length preserved)

Commands:

yarn lint
# → Done (max-warnings 0)

# Text itest requires buck, but JS unit for length preservation:
node -p "'a\u0000b'.length" # → 3

Closes #24129

…t#24129)

Fixes react#24129 where <Text>{'Hello \u0000 World'}</Text> renders as 'Hello' truncated.

Root cause: JS String preserves \u0000, but iOS stringWithUTF8String: and stringWithCString: and Android NewStringUTF use C-string NUL-terminated APIs that stop at embedded \0.

Fix: Replace with length-aware conversions:
- iOS: initWithBytes:length:encoding: (same as correct FollyConvert.mm pattern and MapBufferBuilder memcpy)
- Android: convert UTF-8 std::string with size to UTF-16 via rt.utf16() + NewString, and reverse via GetStringChars + GetStringLength + createFromUtf16

Covers all surfaces (Text, TextInput, any TurboModule string param) across Fabric and old arch.

[GENERAL] [FIXED] - Fix Text truncation when string contains NULL character \u0000 (react#24129)
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 12, 2026
@kulkarni-rohan kulkarni-rohan changed the title Fix Text truncation at NULL character \u0000 on iOS and Android (#24129) Fix Text truncation at NULL character on iOS and Android (#24129) Aug 12, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Aug 12, 2026
@meta-codesync

meta-codesync Bot commented Aug 12, 2026

Copy link
Copy Markdown

@fabriziocucci has imported this pull request. If you are a Meta employee, you can view this in D115707536.

@meta-codesync meta-codesync Bot closed this in 5906cfb Aug 12, 2026
@meta-codesync meta-codesync Bot added the Merged This PR has been merged. label Aug 12, 2026
@meta-codesync

meta-codesync Bot commented Aug 12, 2026

Copy link
Copy Markdown

@fabriziocucci merged this pull request in 5906cfb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. p: Facebook Partner: Facebook Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JavaScript strings with NULL character are not handled properly

1 participant