From 25f5df272fb19657d68914d2c7245779e4f6fede Mon Sep 17 00:00:00 2001 From: Alexander Maryanovsky Date: Wed, 27 Mar 2024 16:21:10 +0200 Subject: [PATCH] Fix 2 crashing bugs in basic string processing for BasicTextField2 --- .../foundation/text2/input/internal/GapBuffer.kt | 4 ++-- .../text2/input/internal/ToCharArray.skiko.kt | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text2/input/internal/GapBuffer.kt b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text2/input/internal/GapBuffer.kt index a9bf8b427191e..044da4d5cd419 100644 --- a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text2/input/internal/GapBuffer.kt +++ b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text2/input/internal/GapBuffer.kt @@ -182,8 +182,8 @@ private class GapBuffer(initBuffer: CharArray, initGapStart: Int, initGapEnd: In * @param builder The output string builder */ fun append(builder: StringBuilder) { - builder.appendRange(buffer, 0, gapStart) - builder.appendRange(buffer, gapEnd, capacity - gapEnd) + builder.appendRange(buffer, startIndex = 0, endIndex = gapStart) + builder.appendRange(value = buffer, startIndex = gapEnd, endIndex = capacity) } /** diff --git a/compose/foundation/foundation/src/skikoMain/kotlin/androidx/compose/foundation/text2/input/internal/ToCharArray.skiko.kt b/compose/foundation/foundation/src/skikoMain/kotlin/androidx/compose/foundation/text2/input/internal/ToCharArray.skiko.kt index 05b5cc866db5b..9598c527b36d3 100644 --- a/compose/foundation/foundation/src/skikoMain/kotlin/androidx/compose/foundation/text2/input/internal/ToCharArray.skiko.kt +++ b/compose/foundation/foundation/src/skikoMain/kotlin/androidx/compose/foundation/text2/input/internal/ToCharArray.skiko.kt @@ -36,13 +36,14 @@ internal actual fun CharSequence.toCharArray( endIndex ) else -> { - require(startIndex in indices && endIndex in 0..length) { + rangeCheck(start = startIndex, end = endIndex, length = length) { "Expected source [$startIndex, $endIndex) to be in [0, $length)" } val copyLength = endIndex - startIndex - require( - destinationOffset in destination.indices && - destinationOffset + copyLength in 0..destination.size + rangeCheck( + start = destinationOffset, + end = destinationOffset + copyLength, + length = destination.size ) { "Expected destination [$destinationOffset, ${destinationOffset + copyLength}) " + "to be in [0, ${destination.size})" @@ -54,3 +55,7 @@ internal actual fun CharSequence.toCharArray( } } } + +private inline fun rangeCheck(start: Int, end: Int, length: Int, lazyMessage: () -> String) { + require((start >= 0) && (start <= end) && (end <= length), lazyMessage) +}