From 46354c39380731c377021f5b6ae8d20ab4c1886f Mon Sep 17 00:00:00 2001 From: Aleksey Kondakov <112566801+A-Kimpo@users.noreply.github.com> Date: Mon, 26 Oct 2026 02:46:39 +0500 Subject: [PATCH 1/2] Fix: unmounted secureTextEntry TextInput retained by the measurement cache Added logic to strip specific spans from the text buffer to prevent memory leaks. --- .../com/facebook/react/views/textinput/ReactEditText.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt index 5858f865273f..73b4de50289a 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt @@ -32,6 +32,8 @@ import android.text.TextUtils import android.text.TextWatcher import android.text.method.KeyListener import android.text.method.QwertyKeyListener +import android.text.style.CharacterStyle +import android.text.style.ParagraphStyle import android.util.TypedValue import android.view.ActionMode import android.view.DragEvent @@ -1242,6 +1244,12 @@ public open class ReactEditText public constructor(context: Context) : AppCompat } catch (e: IndexOutOfBoundsException) { logSoftException(TAG, e) } + // The copy only feeds measurement, so drop editor bookkeeping spans. Some of them reference + // this view's Editable (PasswordTransformationMethod.Visible), and the cache is evicted only + // from finalize(), so keeping them would keep this view reachable for good. + stripSpansOfKind(sb, Any::class.java) { span -> + span !is CharacterStyle && span !is ParagraphStyle && span !is ReactSpan + } } // If we don't have text, make sure we have *something* to measure. From 95d59327bba62bbd79bd4e89c049978ee6921fc1 Mon Sep 17 00:00:00 2001 From: Alex Kimpo Date: Mon, 16 Nov 2026 10:03:22 +0100 Subject: [PATCH 2/2] Add test for secure TextInput spans in the measurement cache --- .../ReactEditTextMeasurementCacheTest.kt | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactEditTextMeasurementCacheTest.kt diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactEditTextMeasurementCacheTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactEditTextMeasurementCacheTest.kt new file mode 100644 index 000000000000..d34323d13b86 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactEditTextMeasurementCacheTest.kt @@ -0,0 +1,122 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.views.textinput + +import android.provider.Settings +import android.text.Spannable +import android.text.method.PasswordTransformationMethod +import android.text.style.AbsoluteSizeSpan +import android.text.style.CharacterStyle +import android.text.style.ParagraphStyle +import android.util.DisplayMetrics +import androidx.core.content.res.ResourcesCompat.ID_NULL +import com.facebook.react.bridge.ReactTestHelper.createTestReactApplicationContext +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests +import com.facebook.react.uimanager.DisplayMetricsHolder +import com.facebook.react.uimanager.StateWrapper +import com.facebook.react.uimanager.ThemedReactContext +import com.facebook.react.views.text.TextLayoutManager +import com.facebook.react.views.text.internal.span.ReactSpan +import com.facebook.testutils.shadows.ShadowSoLoader +import org.assertj.core.api.Assertions.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.kotlin.mock +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(shadows = [ShadowSoLoader::class]) +class ReactEditTextMeasurementCacheTest { + + private lateinit var view: ReactEditText + + @Before + fun setup() { + ReactNativeFeatureFlagsForTests.setUp() + val context = createTestReactApplicationContext(RuntimeEnvironment.getApplication()) + val themedContext = ThemedReactContext(context, context.baseContext, null, ID_NULL) + DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics()) + view = ReactTextInputManager().createViewInstance(themedContext) + // The measurement cache is only written for a Fabric view that has a tag. + view.id = VIEW_TAG + view.stateWrapper = mock() + } + + @After + fun tearDown() { + TextLayoutManager.deleteCachedSpannableForTag(VIEW_TAG) + } + + @Test + fun cachedCopyOfSecureTextHasNoSpanReferencingTheEditable() { + // "Show password" makes PasswordTransformationMethod mark the last typed character with a + // Visible span, which holds a reference to the view's Editable. + Settings.System.putInt( + RuntimeEnvironment.getApplication().contentResolver, + Settings.System.TEXT_SHOW_PASSWORD, + 1, + ) + view.transformationMethod = PasswordTransformationMethod.getInstance() + + val editable = checkNotNull(view.text) + editable.append("a") + + // ViewReference also belongs to PasswordTransformationMethod, but it is a NoCopySpan. + // Require the Visible span that holds a strong reference to the Editable. + val visibleSpans = + spansOf(editable).filter { + it.javaClass.name == "android.text.method.PasswordTransformationMethod\$Visible" + } + assertThat(visibleSpans).hasSize(1) + + // Refresh the cache while the first character's Visible span is still in the Editable. + editable.append("b") + + // Only spans that affect measurement may reach the cache. + val cachedSpans = spansOf(cachedSpannable()) + assertThat(cachedSpans).doesNotContainAnyElementsOf(visibleSpans) + assertThat(cachedSpans).isNotEmpty + assertThat(cachedSpans).allMatch { isMeasurementSpan(it) } + } + + @Test + fun cachedCopyKeepsStyleSpans() { + val editable = checkNotNull(view.text) + editable.append("abc") + val sizeSpan = AbsoluteSizeSpan(40) + editable.setSpan(sizeSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) + editable.append("d") + + assertThat(spansOf(cachedSpannable())).contains(sizeSpan) + } + + private fun isMeasurementSpan(span: Any): Boolean = + span is CharacterStyle || span is ParagraphStyle || span is ReactSpan + + private fun spansOf(text: Spannable): List = + text.getSpans(0, text.length, Any::class.java).toList() + + @Suppress("UNCHECKED_CAST") + private fun cachedSpannable(): Spannable { + val cache = + TextLayoutManager::class + .java + .getDeclaredField("tagToSpannableCache") + .apply { isAccessible = true } + .get(TextLayoutManager) as Map + return checkNotNull(cache[VIEW_TAG]) + } + + private companion object { + const val VIEW_TAG = 42 + } +}