Skip to content

Commit c604fed

Browse files
authored
Merge pull request scala/scala#10118 from lrytz/deser
Prevent Function0 execution during LazyList deserialization
2 parents bd4a7d6 + 3547d79 commit c604fed

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

library/src/scala/collection/immutable/LazyList.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import java.lang.{StringBuilder => JStringBuilder}
1919

2020
import scala.annotation.tailrec
2121
import scala.collection.generic.SerializeEnd
22-
import scala.collection.mutable.{ArrayBuffer, Builder, ReusableBuilder, StringBuilder}
22+
import scala.collection.mutable.{Builder, ReusableBuilder, StringBuilder}
2323
import scala.language.implicitConversions
2424
import scala.runtime.Statics
2525

@@ -1353,7 +1353,7 @@ object LazyList extends SeqFactory[LazyList] {
13531353
private[this] def writeObject(out: ObjectOutputStream): Unit = {
13541354
out.defaultWriteObject()
13551355
var these = coll
1356-
while(these.knownNonEmpty) {
1356+
while (these.knownNonEmpty) {
13571357
out.writeObject(these.head)
13581358
these = these.tail
13591359
}
@@ -1363,14 +1363,17 @@ object LazyList extends SeqFactory[LazyList] {
13631363

13641364
private[this] def readObject(in: ObjectInputStream): Unit = {
13651365
in.defaultReadObject()
1366-
val init = new ArrayBuffer[A]
1366+
val init = new mutable.ListBuffer[A]
13671367
var initRead = false
13681368
while (!initRead) in.readObject match {
13691369
case SerializeEnd => initRead = true
13701370
case a => init += a.asInstanceOf[A]
13711371
}
13721372
val tail = in.readObject().asInstanceOf[LazyList[A]]
1373-
coll = init ++: tail
1373+
// scala/scala#10118: caution that no code path can evaluate `tail.state`
1374+
// before the resulting LazyList is returned
1375+
val it = init.toList.iterator
1376+
coll = newLL(stateFromIteratorConcatSuffix(it)(tail.state))
13741377
}
13751378

13761379
private[this] def readResolve(): Any = coll

0 commit comments

Comments
 (0)