Fix RuntimeException in SkewMatrixHelper when transform map is empty

Summary:
X-link: https://github.com/facebook/react-native/pull/57125

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

Reviewed By: Abbondanzo

Differential Revision: D107763077

fbshipit-source-id: d61819e875e2334c6d6b121aba1876ec3153c002
This commit is contained in:
Pieter De Baets authored and meta-codesync[bot] committed 2026-06-10 08:54:14 -07:00
1 parent a8d16b623e
commit 9e3a0df61e
2 files changed
+27 -2

No files matched your search

@@ -36,7 +36,9 @@ internal object SkewMatrixHelper {
for (i in 0 until transforms.size()) {
if (transforms.getType(i) != ReadableType.Map) continue
val map = transforms.getMap(i) ?: continue
val type = map.keySetIterator().nextKey()
val iterator = map.keySetIterator()
if (!iterator.hasNextKey()) continue
val type = iterator.nextKey()
when (type) {
"matrix",
"perspective",
@@ -98,7 +100,9 @@ internal object SkewMatrixHelper {
for (i in 0 until transforms.size()) {
if (transforms.getType(i) != ReadableType.Map) continue
val map = transforms.getMap(i) ?: continue
val type = map.keySetIterator().nextKey()
val iterator = map.keySetIterator()
if (!iterator.hasNextKey()) continue
val type = iterator.nextKey()
when (type) {
"rotate",
"rotateZ" ->
@@ -180,6 +180,27 @@ class SkewMatrixHelperTest {
assertThat(values[Matrix.MTRANS_X]).isCloseTo(0f, within(EPSILON))
}
@Test
fun emptyMapInTransforms_doesNotThrow() {
// Regression test for T274692242: an empty map in the transforms array caused
// NoSuchElementException from keySetIterator().nextKey() without hasNextKey() guard.
val transforms =
JavaOnlyArray.of(
JavaOnlyMap(), // empty map — previously crashed
JavaOnlyMap.of("skewX", "20deg"),
)
// isAffine2DTransformWithSkew must not throw and should still detect skew
assertThat(SkewMatrixHelper.isAffine2DTransformWithSkew(transforms)).isTrue()
// buildAffine2DMatrix must not throw when encountering the empty map
val matrix = SkewMatrixHelper.buildAffine2DMatrix(transforms, 100f, 100f, null)
val values = matrixValues(matrix)
// The empty map is skipped; only the skewX entry contributes
val tan20 = Math.tan(Math.toRadians(20.0)).toFloat()
assertThat(values[Matrix.MSKEW_X]).isCloseTo(tan20, within(EPSILON))
}
private fun matrixValues(matrix: Matrix): FloatArray {
val values = FloatArray(9)
matrix.getValues(values)