Repository navigation
Commit d8789c6
Stop non-finite elevation and scale values from crashing Android 9 and later (#59314)
Summary:
A view whose `elevation` is `NaN` or infinite ends the app on Android 9 and later, and so do a `DrawerLayoutAndroid`'s `elevation` and the deprecated `scaleX` and `scaleY` props. From API 28, `View.setElevation`, `setScaleX` and `setScaleY` pass their value through `sanitizeFloatPropertyValue` ([`View.java`, line 20100](https://android.googlesource.com/platform/frameworks/base/+/refs/tags/android-16.0.0_r1/core/java/android/view/View.java#20100)), which throws `IllegalArgumentException: Cannot set 'elevation' to Float.NaN` for an app targeting API 28 or later ([line 5903](https://android.googlesource.com/platform/frameworks/base/+/refs/tags/android-16.0.0_r1/core/java/android/view/View.java#5903)). `BaseViewManager.setTransformProperty` already reads every value it hands those setters through its own `sanitizeFloatPropertyValue`, but `BaseViewManager.setElevation`, the deprecated scale setters and `ReactDrawerLayoutManager.setElevation`, which hands `DrawerLayout.setDrawerElevation` its value without `super`, do not.
This moves the helper to `com.facebook.react.uimanager.internal.FloatPropertySanitizer`, which the drawer can reach and the public API leaves out, and reads those values through it — the elevations after the dp-to-pixel conversion, so an infinite elevation isn't multiplied back to infinity on a density above 1, and a finite one that overflows in pixels is caught too. A `NaN` takes 0 and ±∞ ±`Float.MAX_VALUE`: the values the framework itself gives an app that does not opt into the throw. A finite value costs the helper's `NaN` test and its two bounds, in setters that run when the prop changes.
#59229 makes a transform holding a non-finite value ignored, as the web does.
## Changelog:
[ANDROID] [FIXED] - A `NaN` or infinite `elevation`, or deprecated `scaleX` or `scaleY`, no longer crashes the app on Android 9 and later
Pull Request resolved: #59314
Test Plan:
- `BaseViewManagerTest`: an elevation of `NaN`, `Infinity`, `-Infinity` and `1e39` through the props map, a `NaN` after a finite one, a finite elevation that overflows only in pixels at density 3, and `NaN` and infinite `scaleX` and `scaleY`. `ReactDrawerLayoutManagerTest` (new): a drawer at a `NaN` and an infinite elevation, and at a `Float.MAX_VALUE` elevation at density 3. On `main` the 11 new cases fail on `View`'s own refusal (`Cannot set 'elevation' to Float.NaN`, `Cannot set 'scaleX' to Float.NaN`, …); with this change they pass, and so does the whole ReactAndroid JVM suite (776 tests).
- google-java-format and ktfmt pass.
- To reproduce, on Android 9 or later:
```jsx
<View style={{width: 48, height: 48, backgroundColor: 'white', elevation: NaN}} />
```
On `main` the app ends with `Cannot set 'elevation' to Float.NaN`, as it does for a `DrawerLayoutAndroid` at `elevation: NaN`, and with `Cannot set 'scaleX' to Float.NaN` for a `View` given `scaleX={NaN}`; with this change each draws. I ran RNTester release builds of `main` and of this change on an Android 14 phone with a root component rendering every case; a screenshot and the scene are in the [evidence comment](#59314 (comment)).
Reviewed By: Abbondanzo
Differential Revision: D126883976
Pulled By: javache
fbshipit-source-id: e87a85ab56a50c5bbb94891888ced851d9a453b91 parent 9de6471 commit d8789c6
5 files changed
Lines changed: 171 additions & 27 deletions
File tree
- packages/react-native/ReactAndroid/src
- main/java/com/facebook/react
- uimanager
- internal
- views/drawer
- test/java/com/facebook/react
- uimanager
- views/drawer
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
298 | 300 | | |
299 | 301 | | |
300 | 302 | | |
301 | | - | |
| 303 | + | |
302 | 304 | | |
303 | 305 | | |
304 | 306 | | |
| |||
553 | 555 | | |
554 | 556 | | |
555 | 557 | | |
556 | | - | |
| 558 | + | |
557 | 559 | | |
558 | 560 | | |
559 | 561 | | |
560 | 562 | | |
561 | 563 | | |
562 | | - | |
| 564 | + | |
563 | 565 | | |
564 | 566 | | |
565 | 567 | | |
| |||
719 | 721 | | |
720 | 722 | | |
721 | 723 | | |
722 | | - | |
723 | | - | |
724 | | - | |
725 | | - | |
726 | | - | |
727 | | - | |
728 | | - | |
729 | | - | |
730 | | - | |
731 | | - | |
732 | | - | |
733 | | - | |
734 | | - | |
735 | | - | |
736 | | - | |
737 | | - | |
738 | | - | |
739 | | - | |
740 | | - | |
741 | | - | |
742 | | - | |
743 | | - | |
744 | | - | |
745 | 724 | | |
746 | 725 | | |
747 | 726 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| |||
142 | 143 | | |
143 | 144 | | |
144 | 145 | | |
145 | | - | |
| 146 | + | |
146 | 147 | | |
147 | 148 | | |
148 | 149 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
321 | 322 | | |
322 | 323 | | |
323 | 324 | | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
324 | 397 | | |
325 | 398 | | |
326 | 399 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
0 commit comments