Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 2 crashing bugs in basic string processing for BasicTextField2 #1227

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
Expand All @@ -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)
}
Loading