Skip to content

Commit 3a2127d

Browse files
committed
Fix infinite-loop when using Jar#iterator
So far, Jar was defined as an Iterable implementation that overrides `foreach` and then define `override def iterator = this.toList.iterator` That was OK with the 2.12 stdlib, but stackoverflows with the 2.13 one due to `toList` now being implemented in terms of `iterator`. Any way, it turns out that we don't really need Jar to be an Iterable, so the simplest fix was to just remove these methods, and add back a `toList` for the one usesite that needed it (not very efficient, but not any worse than before).
1 parent 81f2467 commit 3a2127d

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

compiler/src/dotty/tools/dotc/Driver.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ class Driver {
7474
val (classPaths, classNames) = fileNames0.flatMap { name =>
7575
val path = Paths.get(name)
7676
if (name.endsWith(".jar")) {
77-
new dotty.tools.io.Jar(File(name)).iterator.collect {
77+
new dotty.tools.io.Jar(File(name)).toList.collect {
7878
case e if e.getName.endsWith(".tasty") =>
7979
(name, e.getName.stripSuffix(".tasty").replace("/", "."))
80-
}.toList
80+
}
8181
}
8282
else if (!name.endsWith(".tasty"))
8383
("", name) :: Nil

compiler/src/dotty/tools/io/Jar.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import scala.annotation.tailrec
3535
// static Attributes.Name SPECIFICATION_VENDOR
3636
// static Attributes.Name SPECIFICATION_VERSION
3737

38-
class Jar(file: File) extends Iterable[JarEntry] {
38+
class Jar(file: File) {
3939
def this(jfile: JFile) = this(File(jfile.toPath))
4040
def this(path: String) = this(File(path))
4141

@@ -64,10 +64,9 @@ class Jar(file: File) extends Iterable[JarEntry] {
6464
new JarWriter(file, Jar.WManifest.apply(mainAttrs: _*).underlying)
6565
}
6666

67-
override def foreach[U](f: JarEntry => U): Unit = withJarInput { in =>
68-
Iterator continually in.getNextJarEntry() takeWhile (_ != null) foreach f
67+
def toList: List[JarEntry] = withJarInput { in =>
68+
Iterator.continually(in.getNextJarEntry()).takeWhile(_ != null).toList
6969
}
70-
override def iterator: Iterator[JarEntry] = this.toList.iterator
7170

7271
def getEntryStream(entry: JarEntry): java.io.InputStream = jarFile getInputStream entry match {
7372
case null => errorFn("No such entry: " + entry) ; null

0 commit comments

Comments
 (0)