From f70c26087c06c177051418597ee3bb10d6d5a4e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Costa?= Date: Tue, 16 Apr 2024 03:41:21 +0100 Subject: [PATCH] chore: Fix ByteIterator#indexWhere (#1282) It would blow up when from was out of bounds. --- .../test/scala/org/apache/pekko/util/ByteStringSpec.scala | 5 +++++ .../main/scala-2.12/org/apache/pekko/util/ByteIterator.scala | 2 +- .../main/scala-2.13/org/apache/pekko/util/ByteIterator.scala | 2 +- .../main/scala-3/org/apache/pekko/util/ByteIterator.scala | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringSpec.scala b/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringSpec.scala index d7233507597..aca2f242ed9 100644 --- a/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringSpec.scala +++ b/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringSpec.scala @@ -923,6 +923,11 @@ class ByteStringSpec extends AnyWordSpec with Matchers with Checkers { likeVector(a) { _.indexWhere(_ == b) } } } + "calling indexWhere(p, idx)" in { + check { (a: ByteString, b: Byte, idx: Int) => + likeVector(a) { _.indexWhere(_ == b, math.max(0, idx)) } + } + } "calling indexOf" in { check { (a: ByteString, b: Byte) => likeVector(a) { _.indexOf(b) } diff --git a/actor/src/main/scala-2.12/org/apache/pekko/util/ByteIterator.scala b/actor/src/main/scala-2.12/org/apache/pekko/util/ByteIterator.scala index 46d50e2e6cd..03c536d5559 100644 --- a/actor/src/main/scala-2.12/org/apache/pekko/util/ByteIterator.scala +++ b/actor/src/main/scala-2.12/org/apache/pekko/util/ByteIterator.scala @@ -470,7 +470,7 @@ abstract class ByteIterator extends BufferedIterator[Byte] { override def indexWhere(p: Byte => Boolean): Int = indexWhere(p, 0) override def indexWhere(p: Byte => Boolean, from: Int): Int = { var index = 0 - while (index < from) { + while (index < from && hasNext) { next() index += 1 } diff --git a/actor/src/main/scala-2.13/org/apache/pekko/util/ByteIterator.scala b/actor/src/main/scala-2.13/org/apache/pekko/util/ByteIterator.scala index af64128e65a..412ae84c099 100644 --- a/actor/src/main/scala-2.13/org/apache/pekko/util/ByteIterator.scala +++ b/actor/src/main/scala-2.13/org/apache/pekko/util/ByteIterator.scala @@ -486,7 +486,7 @@ abstract class ByteIterator extends BufferedIterator[Byte] { override def indexWhere(p: Byte => Boolean, from: Int = 0): Int = { var index = 0 - while (index < from) { + while (index < from && hasNext) { next() index += 1 } diff --git a/actor/src/main/scala-3/org/apache/pekko/util/ByteIterator.scala b/actor/src/main/scala-3/org/apache/pekko/util/ByteIterator.scala index 85699fcca71..42d1d83e6aa 100644 --- a/actor/src/main/scala-3/org/apache/pekko/util/ByteIterator.scala +++ b/actor/src/main/scala-3/org/apache/pekko/util/ByteIterator.scala @@ -482,7 +482,7 @@ abstract class ByteIterator extends BufferedIterator[Byte] { override def indexWhere(p: Byte => Boolean, from: Int = 0): Int = { var index = 0 - while (index < from) { + while (index < from && hasNext) { next() index += 1 }