Skip to content

[Android] Fix crash measuring Text with a font size of 0 - #60134

Open
kayahickindev wants to merge 1 commit into
react:mainfrom
kayahickindev:android-text-zero-font-size
Open

kayahickindev wants to merge 1 commit into
react:mainfrom
kayahickindev:android-text-zero-font-size

Conversation

@kayahickindev

Copy link
Copy Markdown

Summary:

On Android, a Text or TextInput whose style resolves to a fontSize of 0 or less crashes the app when it is measured:

java.lang.IllegalArgumentException: FontSize should be a positive value. Current value: 0
    at com.facebook.react.views.text.TextAttributeProps.getLetterSpacing(TextAttributeProps.kt:181)
    at com.facebook.react.views.text.TextLayoutManager.buildSpannableFromFragmentsOptimized(TextLayoutManager.kt:631)
    at com.facebook.react.views.text.TextLayoutManager.createSpannableFromAttributedString(TextLayoutManager.kt:840)

The crash happens whether or not the text sets letterSpacing. iOS has no such check.

The usual way an app gets there is a font size computed from a measured size that starts at 0. A cell that sizes its label from its own width renders once before onLayout fires:

function Cell({label}) {
  const [width, setWidth] = useState(0);
  return (
    <View style={{flex: 1}} onLayout={e => setWidth(e.nativeEvent.layout.width)}>
      <Text style={{fontSize: width - 12}}>{label}</Text>
    </View>
  );
}

That first render lays the Text out with a font size of -12, and Android crashes. react-native-emoji-selector sizes its cells this way (fontSize: colSize - 12), and its users patch it with Math.max(12, ...) to stop the crash (arronhunt/react-native-emoji-selector#70). A JS-driven Animated value for fontSize that starts at 0 or overshoots below it reaches the same code. allowFontScaling and maxFontSizeMultiplier do not change the outcome: 0 scales to 0 and a negative size stays negative, while any positive size rounds up to at least 1px.

The cause is in TextAttributeProps.letterSpacing. TextLayoutManager.buildSpannableFromFragmentsOptimized and buildSpannableFromFragments read it for every fragment to check isNaN(), whether or not letterSpacing was set. The getter converts the spacing to ems by dividing by the font size, and before that it runs require(fontSize > 0), which throws even when there is no spacing to convert. The same throw was in the Java getLetterSpacing() before the Kotlin migration, so this is long-standing rather than a recent regression.

The fix returns NaN when letterSpacing is unset, without looking at the font size, and 0 when the font size is 0 or less, since there is no em to convert the spacing to. Text with a positive font size is unchanged. The getter returns 0 rather than NaN because updateTextPaint assigns its value to TextPaint.letterSpacing whenever letterSpacing is set, so NaN would reach the paint.

Minimal repro:

<Text style={{fontSize: 0}}>Hidden</Text>

Changelog:

[ANDROID] [FIXED] - Fix crash when a Text or TextInput has a fontSize of 0 or less

Test Plan:

Added tests (Robolectric):

  • TextAttributePropsTest: letterSpacingIsUnsetWithZeroFontSize (no letterSpacing, font size 0, expects NaN) and letterSpacingIsZeroWithZeroFontSize (letterSpacing 2, font size 0, expects 0).
  • TextLayoutManagerZeroFontSizeTest: measures a one-fragment attributed string through TextLayoutManager.measureText with font sizes 0 and -12.
./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest \
  --tests 'com.facebook.react.views.text.TextLayoutManagerZeroFontSizeTest' \
  --tests 'com.facebook.react.views.text.TextAttributePropsTest' \
  -Preact.internal.useHermesNightly=true -PreactNativeArchitectures=arm64-v8a

Before the fix (the TextAttributeProps.kt change reverted, tests kept), all four new tests fail:

TextAttributePropsTest > letterSpacingIsZeroWithZeroFontSize FAILED
    java.lang.IllegalArgumentException at TextAttributePropsTest.kt:228
TextAttributePropsTest > letterSpacingIsUnsetWithZeroFontSize FAILED
    java.lang.IllegalArgumentException at TextAttributePropsTest.kt:216
TextLayoutManagerZeroFontSizeTest > text with a negative font size can be measured FAILED
    java.lang.IllegalArgumentException at TextLayoutManagerZeroFontSizeTest.kt:72
TextLayoutManagerZeroFontSizeTest > text with a font size of 0 can be measured FAILED
    java.lang.IllegalArgumentException at TextLayoutManagerZeroFontSizeTest.kt:72

18 tests completed, 4 failed

Each failure is FontSize should be a positive value. Current value: 0 (or -12), thrown from TextAttributeProps.getLetterSpacing.

After the fix, the whole package:

./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest \
  --tests 'com.facebook.react.views.text.*' \
  -Preact.internal.useHermesNightly=true -PreactNativeArchitectures=arm64-v8a

BUILD SUCCESSFUL: 19 suites, 128 tests, 0 failures. On main at f4f8b6a the same command runs 18 suites, 124 tests, 0 failures.

node ./scripts/format-kotlin.js --check (what yarn format-check-kotlin runs) passes, and compileDebugKotlin and compileDebugUnitTestKotlin pass with -PenableWarningsAsErrors=true.

Not run: a device or emulator, and iOS. The tests above are Robolectric unit tests only.

🤖 Generated with Claude Code

Building the spannable for a Text reads TextAttributeProps.letterSpacing
for every fragment, even when no letterSpacing was set. The getter
required a positive font size and threw IllegalArgumentException
otherwise, so a style that resolves fontSize to 0 or less (for example
a size computed from a width that is still 0 before the first layout)
crashed text measurement for Text and TextInput.

Return NaN when letterSpacing is unset, as before, and 0 when the font
size is 0 or less, since there is no em to scale it by.

Changelog:
[ANDROID] [FIXED] - Fix crash when a Text or TextInput has a fontSize of 0 or less

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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 Nov 19, 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 Nov 19, 2026

This branch has not been deployed

No deployments
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. 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.

1 participant