Skip to content

Commit 6f33c61

Browse files
committed
error when reading class file with unknown newer jdk version
[Cherry-picked f430e44][modified]
1 parent f38e5e0 commit 6f33c61

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

compiler/src/dotty/tools/dotc/core/classfile/ClassfileConstants.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ object ClassfileConstants {
1111
inline val JAVA_MINOR_VERSION = 3
1212

1313
inline val JAVA8_MAJOR_VERSION = 52
14+
inline val JAVA_LATEST_MAJOR_VERSION = 65
1415

1516
/** (see http://java.sun.com/docs/books/jvms/second_edition/jvms-clarify.html)
1617
*

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import io.{AbstractFile, ZipArchive}
2525
import scala.util.control.NonFatal
2626

2727
object ClassfileParser {
28+
import ClassfileConstants.*
29+
2830
/** Marker trait for unpicklers that can be embedded in classfiles. */
2931
trait Embedded
3032

@@ -50,6 +52,23 @@ object ClassfileParser {
5052
mapOver(tp)
5153
}
5254
}
55+
56+
private[classfile] def parseHeader(classfile: AbstractFile)(using in: DataReader): Unit = {
57+
val magic = in.nextInt
58+
if (magic != JAVA_MAGIC)
59+
throw new IOException(s"class file '${classfile}' has wrong magic number 0x${toHexString(magic)}, should be 0x${toHexString(JAVA_MAGIC)}")
60+
val minorVersion = in.nextChar.toInt
61+
val majorVersion = in.nextChar.toInt
62+
if ((majorVersion < JAVA_MAJOR_VERSION) ||
63+
((majorVersion == JAVA_MAJOR_VERSION) &&
64+
(minorVersion < JAVA_MINOR_VERSION)))
65+
throw new IOException(
66+
s"class file '${classfile}' has unknown version $majorVersion.$minorVersion, should be at least $JAVA_MAJOR_VERSION.$JAVA_MINOR_VERSION")
67+
if majorVersion > JAVA_LATEST_MAJOR_VERSION then
68+
throw new IOException(
69+
s"class file '${classfile}' has unknown version $majorVersion.$minorVersion, and was compiled by a newer JDK than supported by this Scala version, please update to a newer Scala version.")
70+
}
71+
5372
}
5473

5574
class ClassfileParser(
@@ -82,7 +101,7 @@ class ClassfileParser(
82101
def run()(using Context): Option[Embedded] = try ctx.base.reusableDataReader.withInstance { reader =>
83102
implicit val reader2 = reader.reset(classfile)
84103
report.debuglog("[class] >> " + classRoot.fullName)
85-
parseHeader()
104+
parseHeader(classfile)
86105
this.pool = new ConstantPool
87106
val res = parseClass()
88107
this.pool = null
@@ -96,19 +115,6 @@ class ClassfileParser(
96115
|${Option(e.getMessage).getOrElse("")}""")
97116
}
98117

99-
private def parseHeader()(using in: DataReader): Unit = {
100-
val magic = in.nextInt
101-
if (magic != JAVA_MAGIC)
102-
throw new IOException(s"class file '${classfile}' has wrong magic number 0x${toHexString(magic)}, should be 0x${toHexString(JAVA_MAGIC)}")
103-
val minorVersion = in.nextChar.toInt
104-
val majorVersion = in.nextChar.toInt
105-
if ((majorVersion < JAVA_MAJOR_VERSION) ||
106-
((majorVersion == JAVA_MAJOR_VERSION) &&
107-
(minorVersion < JAVA_MINOR_VERSION)))
108-
throw new IOException(
109-
s"class file '${classfile}' has unknown version $majorVersion.$minorVersion, should be at least $JAVA_MAJOR_VERSION.$JAVA_MINOR_VERSION")
110-
}
111-
112118
/** Return the class symbol of the given name. */
113119
def classNameToSymbol(name: Name)(using Context): Symbol =
114120
val nameStr = name.toString

0 commit comments

Comments
 (0)