Skip to content

Commit 5b1a714

Browse files
committed
SI-9915 Utf8_info are modified UTF8
Adapted from scalac commit 3c5990ce5839f4bdfca8fed7f2c415a72f6a8bd8 by Som Snytt: Use DataInputStream.readUTF to read CONSTANT_Utf8_info. This fixes reading embedded null char and supplementary chars.
1 parent d313143 commit 5b1a714

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Contexts._, Symbols._, Types._, Names._, StdNames._, NameOps._, Scopes._,
77
import SymDenotations._, unpickleScala2.Scala2Unpickler._, Constants._, Annotations._, util.Positions._
88
import NameKinds.{ModuleClassName, DefaultGetterName}
99
import ast.tpd._
10-
import java.io.{ File, IOException }
10+
import java.io.{ ByteArrayInputStream, DataInputStream, File, IOException }
1111
import java.lang.Integer.toHexString
1212
import scala.collection.{ mutable, immutable }
1313
import scala.collection.mutable.{ ListBuffer, ArrayBuffer }
@@ -935,12 +935,16 @@ class ClassfileParser(
935935
case null =>
936936
val start = starts(index)
937937
if (in.buf(start).toInt != CONSTANT_UTF8) errorBadTag(start)
938-
val name = termName(in.buf, start + 3, in.getChar(start + 1))
938+
val len = in.getChar(start + 1).toInt
939+
val name = termName(fromMUTF8(in.buf, start + 1, len))
939940
values(index) = name
940941
name
941942
}
942943
}
943944

945+
private def fromMUTF8(bytes: Array[Byte], offset: Int, len: Int): String =
946+
new DataInputStream(new ByteArrayInputStream(bytes, offset, len)).readUTF
947+
944948
/** Return the name found at given index in the constant pool, with '/' replaced by '.'. */
945949
def getExternalName(index: Int): SimpleTermName = {
946950
if (index <= 0 || len <= index)

tests/run/t9915/C_1.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* javac: -encoding UTF-8
3+
*/
4+
public class C_1 {
5+
public static final String NULLED = "X\000ABC";
6+
public static final String SUPPED = "𐒈𐒝𐒑𐒛𐒐𐒘𐒕𐒖";
7+
8+
public String nulled() {
9+
return C_1.NULLED;
10+
}
11+
public String supped() {
12+
return C_1.SUPPED;
13+
}
14+
public int nulledSize() {
15+
return C_1.NULLED.length();
16+
}
17+
public int suppedSize() {
18+
return C_1.SUPPED.length();
19+
}
20+
}

tests/run/t9915/Test_2.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
object Test extends App {
3+
val c = new C_1
4+
assert(c.nulled == "X\u0000ABC") // "X\000ABC"
5+
assert(c.supped == "𐒈𐒝𐒑𐒛𐒐𐒘𐒕𐒖")
6+
7+
assert(C_1.NULLED == "X\u0000ABC") // "X\000ABC"
8+
assert(C_1.SUPPED == "𐒈𐒝𐒑𐒛𐒐𐒘𐒕𐒖")
9+
10+
assert(C_1.NULLED.size == "XYABC".size)
11+
assert(C_1.SUPPED.codePointCount(0, C_1.SUPPED.length) == 8)
12+
}

0 commit comments

Comments
 (0)