Repository navigation
[Android] Fix crash measuring Text with a font size of 0 - #60134
Open
kayahickindev wants to merge 1 commit into
Open
kayahickindev wants to merge 1 commit into
kayahickindev wants to merge 1 commit into
Conversation
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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary:
On Android, a
TextorTextInputwhose style resolves to afontSizeof 0 or less crashes the app when it is measured: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
onLayoutfires:That first render lays the
Textout with a font size of -12, and Android crashes.react-native-emoji-selectorsizes its cells this way (fontSize: colSize - 12), and its users patch it withMath.max(12, ...)to stop the crash (arronhunt/react-native-emoji-selector#70). A JS-drivenAnimatedvalue forfontSizethat starts at 0 or overshoots below it reaches the same code.allowFontScalingandmaxFontSizeMultiplierdo 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.buildSpannableFromFragmentsOptimizedandbuildSpannableFromFragmentsread it for every fragment to checkisNaN(), whether or notletterSpacingwas set. The getter converts the spacing to ems by dividing by the font size, and before that it runsrequire(fontSize > 0), which throws even when there is no spacing to convert. The same throw was in the JavagetLetterSpacing()before the Kotlin migration, so this is long-standing rather than a recent regression.The fix returns
NaNwhenletterSpacingis 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 thanNaNbecauseupdateTextPaintassigns its value toTextPaint.letterSpacingwheneverletterSpacingis set, soNaNwould reach the paint.Minimal repro:
Changelog:
[ANDROID] [FIXED] - Fix crash when a Text or TextInput has a fontSize of 0 or less
Test Plan:
Added tests (Robolectric):
TextAttributePropsTest:letterSpacingIsUnsetWithZeroFontSize(noletterSpacing, font size 0, expectsNaN) andletterSpacingIsZeroWithZeroFontSize(letterSpacing2, font size 0, expects 0).TextLayoutManagerZeroFontSizeTest: measures a one-fragment attributed string throughTextLayoutManager.measureTextwith font sizes 0 and -12.Before the fix (the
TextAttributeProps.ktchange reverted, tests kept), all four new tests fail:Each failure is
FontSize should be a positive value. Current value: 0(or-12), thrown fromTextAttributeProps.getLetterSpacing.After the fix, the whole package:
BUILD SUCCESSFUL: 19 suites, 128 tests, 0 failures. Onmainat f4f8b6a the same command runs 18 suites, 124 tests, 0 failures.node ./scripts/format-kotlin.js --check(whatyarn format-check-kotlinruns) passes, andcompileDebugKotlinandcompileDebugUnitTestKotlinpass with-PenableWarningsAsErrors=true.Not run: a device or emulator, and iOS. The tests above are Robolectric unit tests only.
🤖 Generated with Claude Code