Skip to content

Commit 44be5ed

Browse files
archiecobbsVicente Romero
authored and
Vicente Romero
committed
8219810: javac throws NullPointerException
Reviewed-by: vromero
1 parent b9758d2 commit 44be5ed

File tree

9 files changed

+194
-2
lines changed

9 files changed

+194
-2
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -2199,18 +2199,23 @@ public void run() {
21992199
/** Read a field.
22002200
*/
22012201
VarSymbol readField() {
2202-
long flags = adjustFieldFlags(nextChar());
2202+
char rawFlags = nextChar();
2203+
long flags = adjustFieldFlags(rawFlags);
22032204
Name name = poolReader.getName(nextChar());
22042205
Type type = poolReader.getType(nextChar());
22052206
VarSymbol v = new VarSymbol(flags, name, type, currentOwner);
22062207
readMemberAttrs(v);
2208+
if (Integer.bitCount(rawFlags & (PUBLIC | PRIVATE | PROTECTED)) > 1 ||
2209+
Integer.bitCount(rawFlags & (FINAL | VOLATILE)) > 1)
2210+
throw badClassFile("illegal.flag.combo", Flags.toString((long)rawFlags), "field", v);
22072211
return v;
22082212
}
22092213

22102214
/** Read a method.
22112215
*/
22122216
MethodSymbol readMethod() {
2213-
long flags = adjustMethodFlags(nextChar());
2217+
char rawFlags = nextChar();
2218+
long flags = adjustMethodFlags(rawFlags);
22142219
Name name = poolReader.getName(nextChar());
22152220
Type type = poolReader.getType(nextChar());
22162221
if (currentOwner.isInterface() &&
@@ -2259,6 +2264,8 @@ MethodSymbol readMethod() {
22592264
validateMethodType(name, m.type);
22602265
setParameters(m, type);
22612266

2267+
if (Integer.bitCount(rawFlags & (PUBLIC | PRIVATE | PROTECTED)) > 1)
2268+
throw badClassFile("illegal.flag.combo", Flags.toString((long)rawFlags), "method", m);
22622269
if ((flags & VARARGS) != 0) {
22632270
final Type last = type.getParameterTypes().last();
22642271
if (last == null || !last.hasTag(ARRAY)) {

src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties

+3
Original file line numberDiff line numberDiff line change
@@ -2505,6 +2505,9 @@ compiler.misc.malformed.vararg.method=\
25052505
compiler.misc.wrong.version=\
25062506
class file has wrong version {0}.{1}, should be {2}.{3}
25072507

2508+
compiler.misc.illegal.flag.combo=\
2509+
class file contains illegal flag combination {0} for {1} {2}
2510+
25082511
#####
25092512

25102513
# 0: type, 1: type or symbol
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//
2+
// class BadFieldFlags {
3+
// protected int my_field;
4+
// }
5+
//
6+
class BadFieldFlags {
7+
0xCAFEBABE;
8+
0; // minor version
9+
63; // version
10+
[] { // Constant Pool
11+
; // first element is empty
12+
Method #2 #3; // #1
13+
class #4; // #2
14+
NameAndType #5 #6; // #3
15+
Utf8 "java/lang/Object"; // #4
16+
Utf8 "<init>"; // #5
17+
Utf8 "()V"; // #6
18+
class #8; // #7
19+
Utf8 "BadFieldFlags"; // #8
20+
Utf8 "my_field"; // #9
21+
Utf8 "I"; // #10
22+
Utf8 "Code"; // #11
23+
Utf8 "LineNumberTable"; // #12
24+
Utf8 "SourceFile"; // #13
25+
Utf8 "BadFieldFlags.java"; // #14
26+
} // Constant Pool
27+
28+
0x0020; // access
29+
#7;// this_cpx
30+
#2;// super_cpx
31+
32+
[] { // Interfaces
33+
} // Interfaces
34+
35+
[] { // Fields
36+
{ // field
37+
0x0006; // access *** SHOULD BE 0x0004 ***
38+
#9; // name_index
39+
#10; // descriptor_index
40+
[] { // Attributes
41+
} // Attributes
42+
}
43+
} // Fields
44+
45+
[] { // Methods
46+
{ // method
47+
0x0000; // access
48+
#5; // name_index
49+
#6; // descriptor_index
50+
[] { // Attributes
51+
Attr(#11) { // Code
52+
1; // max_stack
53+
1; // max_locals
54+
Bytes[]{
55+
0x2AB70001B1;
56+
}
57+
[] { // Traps
58+
} // end Traps
59+
[] { // Attributes
60+
Attr(#12) { // LineNumberTable
61+
[] { // line_number_table
62+
0 1;
63+
}
64+
} // end LineNumberTable
65+
} // Attributes
66+
} // end Code
67+
} // Attributes
68+
}
69+
} // Methods
70+
71+
[] { // Attributes
72+
Attr(#13) { // SourceFile
73+
#14;
74+
} // end SourceFile
75+
} // Attributes
76+
} // end class BadFieldFlags
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* @test /nodynamiccopyright/
3+
* @bug 8219810
4+
* @summary Verify ClassReader detects invalid field access flags combinations
5+
* @build BadFieldFlags
6+
* @compile/fail/ref=BadFieldFlagsTest.out -XDrawDiagnostics BadFieldFlagsTest.java
7+
*/
8+
public class BadFieldFlagsTest {
9+
{
10+
System.out.println(new BadFieldFlags().my_field);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BadFieldFlagsTest.java:10:32: compiler.err.cant.access: BadFieldFlags, (compiler.misc.bad.class.file.header: BadFieldFlags.class, (compiler.misc.illegal.flag.combo: private protected, field, my_field))
2+
1 error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// class BadMethodFlags {
3+
// protected native int my_method();
4+
// }
5+
//
6+
class BadMethodFlags {
7+
0xCAFEBABE;
8+
0; // minor version
9+
63; // version
10+
[] { // Constant Pool
11+
; // first element is empty
12+
Method #2 #3; // #1
13+
class #4; // #2
14+
NameAndType #5 #6; // #3
15+
Utf8 "java/lang/Object"; // #4
16+
Utf8 "<init>"; // #5
17+
Utf8 "()V"; // #6
18+
class #8; // #7
19+
Utf8 "BadMethodFlags"; // #8
20+
Utf8 "Code"; // #9
21+
Utf8 "LineNumberTable"; // #10
22+
Utf8 "my_method"; // #11
23+
Utf8 "()I"; // #12
24+
Utf8 "SourceFile"; // #13
25+
Utf8 "BadMethodFlags.java"; // #14
26+
} // Constant Pool
27+
28+
0x0020; // access
29+
#7;// this_cpx
30+
#2;// super_cpx
31+
32+
[] { // Interfaces
33+
} // Interfaces
34+
35+
[] { // Fields
36+
} // Fields
37+
38+
[] { // Methods
39+
{ // method
40+
0x0000; // access
41+
#5; // name_index
42+
#6; // descriptor_index
43+
[] { // Attributes
44+
Attr(#9) { // Code
45+
1; // max_stack
46+
1; // max_locals
47+
Bytes[]{
48+
0x2AB70001B1;
49+
}
50+
[] { // Traps
51+
} // end Traps
52+
[] { // Attributes
53+
Attr(#10) { // LineNumberTable
54+
[] { // line_number_table
55+
0 1;
56+
}
57+
} // end LineNumberTable
58+
} // Attributes
59+
} // end Code
60+
} // Attributes
61+
}
62+
;
63+
{ // method
64+
0x0105; // access *** SHOULD BE 0x0104 ***
65+
#11; // name_index
66+
#12; // descriptor_index
67+
[] { // Attributes
68+
} // Attributes
69+
}
70+
} // Methods
71+
72+
[] { // Attributes
73+
Attr(#13) { // SourceFile
74+
#14;
75+
} // end SourceFile
76+
} // Attributes
77+
} // end class BadMethodFlags
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* @test /nodynamiccopyright/
3+
* @bug 8219810
4+
* @summary Verify ClassReader detects invalid method access flags combinations
5+
* @build BadMethodFlags
6+
* @compile/fail/ref=BadMethodFlagsTest.out -XDrawDiagnostics BadMethodFlagsTest.java
7+
*/
8+
public class BadMethodFlagsTest {
9+
{
10+
new BadMethodFlags().my_method();
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BadMethodFlagsTest.java:10:13: compiler.err.cant.access: BadMethodFlags, (compiler.misc.bad.class.file.header: BadMethodFlags.class, (compiler.misc.illegal.flag.combo: public protected native, method, my_method()))
2+
1 error

test/langtools/tools/javac/diags/examples.not-yet.txt

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ compiler.misc.fatal.err.cant.locate.meth # Resolve, from Lower
6666
compiler.misc.fatal.err.cant.close # JavaCompiler
6767
compiler.misc.feature.not.supported.in.source.plural # cannot happen (for now)
6868
compiler.misc.file.does.not.contain.package
69+
compiler.misc.illegal.flag.combo # ClassReader
6970
compiler.misc.illegal.start.of.class.file
7071
compiler.misc.inferred.do.not.conform.to.lower.bounds # cannot happen?
7172
compiler.misc.kindname.annotation

0 commit comments

Comments
 (0)