Skip to content

Commit 5d7e63d

Browse files
committed
error when reading class file with unknown newer jdk version
1 parent 090710a commit 5d7e63d

File tree

3 files changed

+19
-28
lines changed

3 files changed

+19
-28
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: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ object ClassfileParser {
5555
}
5656
}
5757

58+
private[classfile] def parseHeader(classfile: AbstractFile)(using in: DataReader): Unit = {
59+
val magic = in.nextInt
60+
if (magic != JAVA_MAGIC)
61+
throw new IOException(s"class file '${classfile}' has wrong magic number 0x${toHexString(magic)}, should be 0x${toHexString(JAVA_MAGIC)}")
62+
val minorVersion = in.nextChar.toInt
63+
val majorVersion = in.nextChar.toInt
64+
if ((majorVersion < JAVA_MAJOR_VERSION) ||
65+
((majorVersion == JAVA_MAJOR_VERSION) &&
66+
(minorVersion < JAVA_MINOR_VERSION)))
67+
throw new IOException(
68+
s"class file '${classfile}' has unknown version $majorVersion.$minorVersion, should be at least $JAVA_MAJOR_VERSION.$JAVA_MINOR_VERSION")
69+
if majorVersion > JAVA_LATEST_MAJOR_VERSION then
70+
throw new IOException(
71+
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.")
72+
}
73+
5874
abstract class AbstractConstantPool(using in: DataReader) {
5975
protected val len = in.nextChar
6076
protected val starts = new Array[Int](len)
@@ -257,7 +273,7 @@ class ClassfileParser(
257273
def run()(using Context): Option[Embedded] = try ctx.base.reusableDataReader.withInstance { reader =>
258274
implicit val reader2 = reader.reset(classfile)
259275
report.debuglog("[class] >> " + classRoot.fullName)
260-
parseHeader()
276+
parseHeader(classfile)
261277
this.pool = new ConstantPool
262278
val res = parseClass()
263279
this.pool = null
@@ -271,19 +287,6 @@ class ClassfileParser(
271287
|${Option(e.getMessage).getOrElse("")}""")
272288
}
273289

274-
private def parseHeader()(using in: DataReader): Unit = {
275-
val magic = in.nextInt
276-
if (magic != JAVA_MAGIC)
277-
throw new IOException(s"class file '${classfile}' has wrong magic number 0x${toHexString(magic)}, should be 0x${toHexString(JAVA_MAGIC)}")
278-
val minorVersion = in.nextChar.toInt
279-
val majorVersion = in.nextChar.toInt
280-
if ((majorVersion < JAVA_MAJOR_VERSION) ||
281-
((majorVersion == JAVA_MAJOR_VERSION) &&
282-
(minorVersion < JAVA_MINOR_VERSION)))
283-
throw new IOException(
284-
s"class file '${classfile}' has unknown version $majorVersion.$minorVersion, should be at least $JAVA_MAJOR_VERSION.$JAVA_MINOR_VERSION")
285-
}
286-
287290
/** Return the class symbol of the given name. */
288291
def classNameToSymbol(name: Name)(using Context): Symbol =
289292
val nameStr = name.toString

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ClassfileTastyUUIDParser(classfile: AbstractFile)(ictx: Context) {
2525

2626
def checkTastyUUID(tastyUUID: UUID)(using Context): Unit = try ctx.base.reusableDataReader.withInstance { reader =>
2727
implicit val reader2 = reader.reset(classfile)
28-
parseHeader()
28+
ClassfileParser.parseHeader(classfile)
2929
this.pool = new ConstantPool
3030
checkTastyAttr(tastyUUID)
3131
this.pool = null
@@ -38,19 +38,6 @@ class ClassfileTastyUUIDParser(classfile: AbstractFile)(ictx: Context) {
3838
|${Option(e.getMessage).getOrElse("")}""")
3939
}
4040

41-
private def parseHeader()(using in: DataReader): Unit = {
42-
val magic = in.nextInt
43-
if (magic != JAVA_MAGIC)
44-
throw new IOException(s"class file '${classfile}' has wrong magic number 0x${toHexString(magic)}, should be 0x${toHexString(JAVA_MAGIC)}")
45-
val minorVersion = in.nextChar.toInt
46-
val majorVersion = in.nextChar.toInt
47-
if ((majorVersion < JAVA_MAJOR_VERSION) ||
48-
((majorVersion == JAVA_MAJOR_VERSION) &&
49-
(minorVersion < JAVA_MINOR_VERSION)))
50-
throw new IOException(
51-
s"class file '${classfile}' has unknown version $majorVersion.$minorVersion, should be at least $JAVA_MAJOR_VERSION.$JAVA_MINOR_VERSION")
52-
}
53-
5441
private def checkTastyAttr(tastyUUID: UUID)(using ctx: Context, in: DataReader): Unit = {
5542
in.nextChar // jflags
5643
in.nextChar // nameIdx

0 commit comments

Comments
 (0)