Skip to content

Commit 7d4f60b

Browse files
asotonajonathan-gibbons
authored andcommitted
8260403: javap should be more robust in the face of invalid class files
Reviewed-by: vromero
1 parent 674be87 commit 7d4f60b

File tree

3 files changed

+165
-1
lines changed

3 files changed

+165
-1
lines changed

src/jdk.jdeps/share/classes/com/sun/tools/javap/ClassWriter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ else if (classFile.isInterface())
229229
}
230230
} catch (ConstantPoolException e) {
231231
print(report(e));
232+
} catch (IllegalStateException e) {
233+
report("Invalid value for Signature attribute: " + e.getMessage());
232234
}
233235
}
234236

@@ -500,7 +502,7 @@ protected void writeMethod(Method m) {
500502
methodExceptions = methodType.throwsTypes;
501503
if (methodExceptions != null && methodExceptions.isEmpty())
502504
methodExceptions = null;
503-
} catch (ConstantPoolException e) {
505+
} catch (ConstantPoolException | IllegalStateException e) {
504506
// report error?
505507
// fall back on standard descriptor
506508
methodType = null;
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
class InvalidSignature {
2+
0xCAFEBABE;
3+
0; // minor version
4+
52; // version
5+
[] { // Constant Pool
6+
; // first element is empty
7+
Method #4 #14; // #1
8+
String #15; // #2
9+
class #16; // #3
10+
class #17; // #4
11+
Utf8 "<init>"; // #5
12+
Utf8 "()V"; // #6
13+
Utf8 "Code"; // #7
14+
Utf8 "LineNumberTable"; // #8
15+
Utf8 "m"; // #9
16+
Utf8 "m2"; // #10
17+
Utf8 "()Ljava/lang/String;"; // #11
18+
Utf8 "SourceFile"; // #12
19+
Utf8 "InvalidSignature.java"; // #13
20+
NameAndType #5 #6; // #14
21+
Utf8 "Hello"; // #15
22+
Utf8 "InvalidSignature"; // #16
23+
Utf8 "java/lang/Object"; // #17
24+
Utf8 "Signature"; // #18
25+
Utf8 "', '"; // #19
26+
} // Constant Pool
27+
28+
0x0020; // access
29+
#3;// this_cpx
30+
#4;// super_cpx
31+
32+
[] { // Interfaces
33+
} // Interfaces
34+
35+
[] { // fields
36+
} // fields
37+
38+
[] { // methods
39+
{ // Member
40+
0x0000; // access
41+
#5; // name_cpx
42+
#6; // sig_cpx
43+
[] { // Attributes
44+
Attr(#7) { // Code
45+
1; // max_stack
46+
1; // max_locals
47+
Bytes[]{
48+
0x2AB70001B1;
49+
};
50+
[] { // Traps
51+
} // end Traps
52+
[] { // Attributes
53+
Attr(#8) { // LineNumberTable
54+
[] { // LineNumberTable
55+
0 1;
56+
}
57+
} // end LineNumberTable
58+
} // Attributes
59+
} // end Code
60+
} // Attributes
61+
} // Member
62+
;
63+
{ // Member
64+
0x0000; // access
65+
#9; // name_cpx
66+
#6; // sig_cpx
67+
[] { // Attributes
68+
Attr(#7) { // Code
69+
0; // max_stack
70+
1; // max_locals
71+
Bytes[]{
72+
0xB1;
73+
};
74+
[] { // Traps
75+
} // end Traps
76+
[] { // Attributes
77+
Attr(#8) { // LineNumberTable
78+
[] { // LineNumberTable
79+
0 2;
80+
}
81+
} // end LineNumberTable
82+
} // Attributes
83+
} // end Code
84+
} // Attributes
85+
} // Member
86+
;
87+
{ // Member
88+
0x0000; // access
89+
#9; // name_cpx
90+
#11; // sig_cpx
91+
[] { // Attributes
92+
Attr(#7) { // Code
93+
1; // max_stack
94+
1; // max_locals
95+
Bytes[]{
96+
0x1202B0;
97+
};
98+
[] { // Traps
99+
} // end Traps
100+
[] { // Attributes
101+
Attr(#8) { // LineNumberTable
102+
[] { // LineNumberTable
103+
0 3;
104+
}
105+
} // end LineNumberTable
106+
} // Attributes
107+
} // end Code
108+
} // Attributes
109+
} // Member
110+
} // methods
111+
112+
[] { // Attributes
113+
Attr(#12) { // SourceFile
114+
#13;
115+
}; // end SourceFile
116+
Attr(#18) { // Signature
117+
#19;
118+
} // end Signature
119+
} // Attributes
120+
} // end class InvalidSignature
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8260403
27+
* @summary javap should be more robust in the face of invalid class files
28+
* @build InvalidSignature
29+
* @run main T8260403
30+
* @modules jdk.jdeps/com.sun.tools.javap
31+
*/
32+
import java.io.PrintWriter;
33+
34+
public class T8260403 {
35+
36+
public static void main(String args[]) throws Exception {
37+
if (com.sun.tools.javap.Main.run(new String[]{"-c", System.getProperty("test.classes") + "/InvalidSignature.class"},
38+
new PrintWriter(System.out)) != 0) {
39+
throw new AssertionError();
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)