Skip to content

Commit f430e44

Browse files
committed
error when reading class file with unknown newer jdk version
1 parent dc012c3 commit f430e44

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
@@ -57,6 +57,22 @@ object ClassfileParser {
5757
}
5858
}
5959

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

276-
private def parseHeader()(using in: DataReader): Unit = {
277-
val magic = in.nextInt
278-
if (magic != JAVA_MAGIC)
279-
throw new IOException(s"class file '${classfile}' has wrong magic number 0x${toHexString(magic)}, should be 0x${toHexString(JAVA_MAGIC)}")
280-
val minorVersion = in.nextChar.toInt
281-
val majorVersion = in.nextChar.toInt
282-
if ((majorVersion < JAVA_MAJOR_VERSION) ||
283-
((majorVersion == JAVA_MAJOR_VERSION) &&
284-
(minorVersion < JAVA_MINOR_VERSION)))
285-
throw new IOException(
286-
s"class file '${classfile}' has unknown version $majorVersion.$minorVersion, should be at least $JAVA_MAJOR_VERSION.$JAVA_MINOR_VERSION")
287-
}
288-
289292
/** Return the class symbol of the given name. */
290293
def classNameToSymbol(name: Name)(using Context): Symbol =
291294
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
@@ -26,7 +26,7 @@ class ClassfileTastyUUIDParser(classfile: AbstractFile)(ictx: Context) {
2626

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

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

0 commit comments

Comments
 (0)