Skip to content

Commit a78a862

Browse files
authored
Merge pull request #179 from filipochnik/gh-178
Fix StackOverflowError in PagedSeq.fromString
2 parents ba30457 + 6e477ce commit a78a862

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

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

+2-15
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,8 @@ object PagedSeq {
4141
fromIterator(source.iterator)
4242

4343
/** Constructs a paged character sequence from a string iterator */
44-
def fromStrings(source: Iterator[String]): PagedSeq[Char] = {
45-
var current: String = ""
46-
def more(data: Array[Char], start: Int, len: Int): Int =
47-
if (current.length != 0) {
48-
val cnt = current.length min len
49-
current.getChars(0, cnt, data, start)
50-
current = current.substring(cnt)
51-
if (cnt == len) cnt
52-
else (more(data, start + cnt, len - cnt) max 0) + cnt
53-
} else if (source.hasNext) {
54-
current = source.next()
55-
more(data, start, len)
56-
} else -1
57-
new PagedSeq(more(_: Array[Char], _: Int, _: Int))
58-
}
44+
def fromStrings(source: Iterator[String]): PagedSeq[Char] =
45+
fromIterator(source.flatMap(_.iterator))
5946

6047
/** Constructs a paged character sequence from a string iterable */
6148
def fromStrings(source: Iterable[String]): PagedSeq[Char] =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package scala.util.parsing.input
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
5+
6+
class gh178 {
7+
8+
@Test
9+
def test: Unit = {
10+
val len = 100000
11+
val i = Iterator.fill(len)("A")
12+
val pagedSeq = PagedSeq.fromStrings(i)
13+
assertEquals(len, pagedSeq.slice(0).length) // should not fail with StackOverflowError
14+
}
15+
}

0 commit comments

Comments
 (0)