Skip to content

Commit ba30457

Browse files
authored
Merge pull request #180 from filipochnik/gh-64
Fix StackOverflowError in Page.latest
2 parents faf66a2 + 334419d commit ba30457

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

shared/src/main/scala/scala/util/parsing/input/PagedSeq.scala

+6-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,12 @@ private class Page[T: ClassTag](val num: Int) {
243243
/** The last page as currently present in the sequence; This can change as more
244244
* elements get appended to the sequence. */
245245
final def latest: Page[T] = {
246-
if (later.next != null) later = later.next.latest
246+
var oldLater = later
247+
while (later.next != null) later = later.next
248+
while (oldLater.next != null) {
249+
oldLater = oldLater.next
250+
oldLater.later = later
251+
}
247252
later
248253
}
249254

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package scala.util.parsing.input
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
6+
class gh64 {
7+
8+
@Test
9+
def test: Unit = {
10+
val len = 4096 * 20000
11+
val i = Iterator.fill(len)(true) // use `true` to make this test more lightweight
12+
val pagedSeq = PagedSeq.fromIterator(i)
13+
pagedSeq.slice(len - 1) // load the whole pagedSeq without caching `latest` element
14+
assertEquals(len, pagedSeq.length) // should not throw StackOverflowError
15+
}
16+
}

0 commit comments

Comments
 (0)