Skip to content

Commit 8cca2bc

Browse files
liskinlrytz
authored andcommitted
Fix NPE in PagedSeq.slice at end of seq
See scala/scala-parser-combinators#70 Basically the same thing as SI-6615, including the fact everything works okay if the PagedSeq is printed before calling slice. It might seem strange that this allows taking slices that start beyond the end, but - this was possible anyway if one forced the entire sequence, and - it is reasonable to be able to take a slice at the very end (not beyond it) and get an empty sequence, which is exactly what StreamReader in scala-parser-combinators does and gets an NPE.
1 parent 0f72dd3 commit 8cca2bc

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/library/scala/collection/immutable/PagedSeq.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ extends scala.collection.AbstractSeq[T]
190190
val e = if (_end == UndeterminedEnd) _end else start + _end
191191
var f = first1
192192
while (f.end <= s && !f.isLast) {
193-
if (f.next eq null) f.addMore(more)
194-
f = f.next
193+
if (f.next eq null) f = f.addMore(more)
194+
else f = f.next
195195
}
196196
// Warning -- not refining `more` means that slices can freely request and obtain
197197
// data outside of their slice. This is part of the design of PagedSeq

test/junit/scala/collection/immutable/PagedSeqTest.scala

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ class PagedSeqTest {
1313
assertEquals(Seq('a'), PagedSeq.fromStrings(List.fill(5000)("a")).slice(4096, 4097))
1414
}
1515

16+
// should not NPE, and should be empty
17+
@Test
18+
def test_SI9480(): Unit = {
19+
assertEquals(Seq(), PagedSeq.fromStrings(List("a")).slice(1))
20+
}
21+
1622
// Slices shouldn't read outside where they belong
1723
@Test
1824
def test_SI6519 {

0 commit comments

Comments
 (0)