Skip to content

Commit 20a35f6

Browse files
javachefacebook-github-bot
authored andcommitted
Fix RuntimeException in SkewMatrixHelper when transform map is empty
Summary: Fixes a crash (`java.lang.RuntimeException` via `NoSuchElementException`) in `SkewMatrixHelper.isAffine2DTransformWithSkew` and `buildAffine2DMatrix` when a transform array contains an empty map entry (no keys). Changelog: [Android][Fixed] Fix crash in SkewMatrixHelper Differential Revision: D107763077
1 parent 40c90ad commit 20a35f6

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/SkewMatrixHelper.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ internal object SkewMatrixHelper {
3636
for (i in 0 until transforms.size()) {
3737
if (transforms.getType(i) != ReadableType.Map) continue
3838
val map = transforms.getMap(i) ?: continue
39-
val type = map.keySetIterator().nextKey()
39+
val iterator = map.keySetIterator()
40+
if (!iterator.hasNextKey()) continue
41+
val type = iterator.nextKey()
4042
when (type) {
4143
"matrix",
4244
"perspective",
@@ -98,7 +100,9 @@ internal object SkewMatrixHelper {
98100
for (i in 0 until transforms.size()) {
99101
if (transforms.getType(i) != ReadableType.Map) continue
100102
val map = transforms.getMap(i) ?: continue
101-
val type = map.keySetIterator().nextKey()
103+
val iterator = map.keySetIterator()
104+
if (!iterator.hasNextKey()) continue
105+
val type = iterator.nextKey()
102106
when (type) {
103107
"rotate",
104108
"rotateZ" ->

packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SkewMatrixHelperTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,27 @@ class SkewMatrixHelperTest {
180180
assertThat(values[Matrix.MTRANS_X]).isCloseTo(0f, within(EPSILON))
181181
}
182182

183+
@Test
184+
fun emptyMapInTransforms_doesNotThrow() {
185+
// Regression test for T274692242: an empty map in the transforms array caused
186+
// NoSuchElementException from keySetIterator().nextKey() without hasNextKey() guard.
187+
val transforms =
188+
JavaOnlyArray.of(
189+
JavaOnlyMap(), // empty map — previously crashed
190+
JavaOnlyMap.of("skewX", "20deg"),
191+
)
192+
193+
// isAffine2DTransformWithSkew must not throw and should still detect skew
194+
assertThat(SkewMatrixHelper.isAffine2DTransformWithSkew(transforms)).isTrue()
195+
196+
// buildAffine2DMatrix must not throw when encountering the empty map
197+
val matrix = SkewMatrixHelper.buildAffine2DMatrix(transforms, 100f, 100f, null)
198+
val values = matrixValues(matrix)
199+
// The empty map is skipped; only the skewX entry contributes
200+
val tan20 = Math.tan(Math.toRadians(20.0)).toFloat()
201+
assertThat(values[Matrix.MSKEW_X]).isCloseTo(tan20, within(EPSILON))
202+
}
203+
183204
private fun matrixValues(matrix: Matrix): FloatArray {
184205
val values = FloatArray(9)
185206
matrix.getValues(values)

0 commit comments

Comments
 (0)