Skip to content

Commit cc68080

Browse files
authored
Fix StringIndexOutOfBounds when a snapshot has an "empty" before (#530 fixes #529)
2 parents 2dbfd2d + ffc20fd commit cc68080

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

jvm/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
## [Unreleased]
1414
### Fixed
1515
- Off-by-one in the error message for a VCR key mismatch. ([#526](https://github.com/diffplug/selfie/pull/526))
16+
- Fix `StringIndexOutOfBoundsException` when an empty snapshot had a facet added. (fixes [#529](https://github.com/diffplug/selfie/issues/529))
1617

1718
## [2.5.1] - 2025-03-04
1819
### Fixed

jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/SelfieImplementations.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 DiffPlug
2+
* Copyright (C) 2024-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -212,7 +212,7 @@ class StringSelfie(
212212
* Returns a serialized form of only the given facets if they are available, silently omits missing
213213
* facets.
214214
*/
215-
private fun serializeOnlyFacets(snapshot: Snapshot, keys: Collection<String>): String {
215+
internal fun serializeOnlyFacets(snapshot: Snapshot, keys: Collection<String>): String {
216216
val writer = StringBuilder()
217217
for (key in keys) {
218218
if (key.isEmpty()) {
@@ -226,7 +226,10 @@ private fun serializeOnlyFacets(snapshot: Snapshot, keys: Collection<String>): S
226226
// this codepath is triggered by the `key.isEmpty()` line above
227227
writer.subSequence(EMPTY_KEY_AND_FACET.length, writer.length - 1).toString()
228228
} else {
229-
writer.setLength(writer.length - 1)
229+
// Check if the writer is empty to avoid StringIndexOutOfBoundsException
230+
if (writer.length > 0) {
231+
writer.setLength(writer.length - 1)
232+
}
230233
writer.toString()
231234
}
232235
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (C) 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.selfie
17+
18+
import io.kotest.matchers.shouldBe
19+
import kotlin.test.Test
20+
21+
class SelfieImplementationsTest {
22+
@Test
23+
fun issue_529() {
24+
val empty = Snapshot.of("")
25+
val emptyPlusFacet = Snapshot.of("").plusFacet("new-facet", "new-facet-value")
26+
val mismathedKeys = listOf("new-facet")
27+
serializeOnlyFacets(empty, mismathedKeys) shouldBe ""
28+
serializeOnlyFacets(emptyPlusFacet, mismathedKeys) shouldBe "╔═ [new-facet] ═╗\nnew-facet-value"
29+
}
30+
}

0 commit comments

Comments
 (0)