Skip to content

Commit 92d3f83

Browse files
authored
Fix ByteIterator#indexWhere (#1285)
It would blow up when from was out of bounds.
1 parent a324eee commit 92d3f83

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

actor-tests/src/test/scala/org/apache/pekko/util/ByteStringSpec.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,11 @@ class ByteStringSpec extends AnyWordSpec with Matchers with Checkers {
922922
likeVector(a) { _.indexWhere(_ == b) }
923923
}
924924
}
925+
"calling indexWhere(p, idx)" in {
926+
check { (a: ByteString, b: Byte, idx: Int) =>
927+
likeVector(a) { _.indexWhere(_ == b, math.max(0, idx)) }
928+
}
929+
}
925930
"calling indexOf" in {
926931
check { (a: ByteString, b: Byte) =>
927932
likeVector(a) { _.indexOf(b) }

actor/src/main/scala-2.12/org/apache/pekko/util/ByteIterator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ abstract class ByteIterator extends BufferedIterator[Byte] {
470470
override def indexWhere(p: Byte => Boolean): Int = indexWhere(p, 0)
471471
override def indexWhere(p: Byte => Boolean, from: Int): Int = {
472472
var index = 0
473-
while (index < from) {
473+
while (index < from && hasNext) {
474474
next()
475475
index += 1
476476
}

actor/src/main/scala-2.13/org/apache/pekko/util/ByteIterator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ abstract class ByteIterator extends BufferedIterator[Byte] {
486486

487487
override def indexWhere(p: Byte => Boolean, from: Int = 0): Int = {
488488
var index = 0
489-
while (index < from) {
489+
while (index < from && hasNext) {
490490
next()
491491
index += 1
492492
}

actor/src/main/scala-3/org/apache/pekko/util/ByteIterator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ abstract class ByteIterator extends BufferedIterator[Byte] {
482482

483483
override def indexWhere(p: Byte => Boolean, from: Int = 0): Int = {
484484
var index = 0
485-
while (index < from) {
485+
while (index < from && hasNext) {
486486
next()
487487
index += 1
488488
}

0 commit comments

Comments
 (0)