Skip to content

Commit 2a38791

Browse files
committedSep 14, 2022
8292755: Non-default method in interface leads to a stack overflow in JShell
Reviewed-by: vromero
1 parent 8351b30 commit 2a38791

File tree

3 files changed

+186
-9
lines changed

3 files changed

+186
-9
lines changed
 

‎src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -1173,15 +1173,16 @@ public void visitMethodDef(JCMethodDecl tree) {
11731173
}
11741174
if (isDefaultMethod || (tree.sym.flags() & (ABSTRACT | NATIVE)) == 0)
11751175
log.error(tree.pos(), Errors.MissingMethBodyOrDeclAbstract);
1176-
} else if ((tree.sym.flags() & (ABSTRACT|DEFAULT|PRIVATE)) == ABSTRACT) {
1177-
if ((owner.flags() & INTERFACE) != 0) {
1178-
log.error(tree.body.pos(), Errors.IntfMethCantHaveBody);
1179-
} else {
1180-
log.error(tree.pos(), Errors.AbstractMethCantHaveBody);
1181-
}
1182-
} else if ((tree.mods.flags & NATIVE) != 0) {
1183-
log.error(tree.pos(), Errors.NativeMethCantHaveBody);
11841176
} else {
1177+
if ((tree.sym.flags() & (ABSTRACT|DEFAULT|PRIVATE)) == ABSTRACT) {
1178+
if ((owner.flags() & INTERFACE) != 0) {
1179+
log.error(tree.body.pos(), Errors.IntfMethCantHaveBody);
1180+
} else {
1181+
log.error(tree.pos(), Errors.AbstractMethCantHaveBody);
1182+
}
1183+
} else if ((tree.mods.flags & NATIVE) != 0) {
1184+
log.error(tree.pos(), Errors.NativeMethCantHaveBody);
1185+
}
11851186
// Add an implicit super() call unless an explicit call to
11861187
// super(...) or this(...) is given
11871188
// or we are compiling class java.lang.Object.

‎test/langtools/jdk/jshell/ClassesTest.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8145239 8129559 8080354 8189248 8010319 8246353 8247456 8282160
26+
* @bug 8145239 8129559 8080354 8189248 8010319 8246353 8247456 8282160 8292755
2727
* @summary Tests for EvaluationState.classes
2828
* @build KullaTesting TestingInputStream ExpectedDiagnostic
2929
* @run testng ClassesTest
@@ -360,4 +360,18 @@ public void run() {}
360360
ste(classKey, Status.RECOVERABLE_NOT_DEFINED, Status.VALID, true, null));
361361
}
362362

363+
public void testDefaultMethodInInterface() {
364+
assertEvalFail("""
365+
interface C {
366+
public void run() {
367+
try {
368+
throw IllegalStateException();
369+
} catch (Throwable t) {
370+
throw new RuntimeException(t);
371+
}
372+
}
373+
}
374+
""");
375+
}
376+
363377
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright (c) 2022, 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 8292755
27+
* @summary Verify error recovery related to method modifiers.
28+
* @library /tools/lib
29+
* @modules jdk.compiler/com.sun.tools.javac.api
30+
* jdk.compiler/com.sun.tools.javac.main
31+
* jdk.jdeps/com.sun.tools.classfile
32+
* @build toolbox.ToolBox toolbox.JavacTask
33+
* @run main MethodModifiers
34+
*/
35+
36+
import java.nio.file.Path;
37+
import java.util.List;
38+
import java.util.Objects;
39+
40+
import toolbox.JavacTask;
41+
import toolbox.Task.Expect;
42+
import toolbox.Task.OutputKind;
43+
import toolbox.TestRunner;
44+
import toolbox.ToolBox;
45+
46+
public class MethodModifiers extends TestRunner {
47+
48+
ToolBox tb;
49+
50+
public MethodModifiers() {
51+
super(System.err);
52+
tb = new ToolBox();
53+
}
54+
55+
public static void main(String[] args) throws Exception {
56+
MethodModifiers t = new MethodModifiers();
57+
t.runTests();
58+
}
59+
60+
@Test
61+
public void testNonDefaultMethodInterface() throws Exception {
62+
String code = """
63+
interface Test {
64+
void test() {
65+
try {
66+
unresolvable();
67+
} catch (Throwable t) {
68+
throw new RuntimeException(t);
69+
}
70+
}
71+
}
72+
""";
73+
Path curPath = Path.of(".");
74+
List<String> actual = new JavacTask(tb)
75+
.options("-XDrawDiagnostics",
76+
"-XDshould-stop.at=FLOW",
77+
"-XDdev")
78+
.sources(code)
79+
.outdir(curPath)
80+
.run(Expect.FAIL)
81+
.getOutputLines(OutputKind.DIRECT);
82+
83+
List<String> expected = List.of(
84+
"Test.java:2:17: compiler.err.intf.meth.cant.have.body",
85+
"Test.java:4:13: compiler.err.cant.resolve.location.args: kindname.method, unresolvable, , , (compiler.misc.location: kindname.interface, Test, null)",
86+
"2 errors"
87+
);
88+
89+
if (!Objects.equals(actual, expected)) {
90+
error("Expected: " + expected + ", but got: " + actual);
91+
}
92+
}
93+
94+
@Test
95+
public void testAbstractMethodWithBody() throws Exception {
96+
String code = """
97+
abstract class Test {
98+
abstract void test() {
99+
try {
100+
unresolvable();
101+
} catch (Throwable t) {
102+
throw new RuntimeException(t);
103+
}
104+
}
105+
}
106+
""";
107+
Path curPath = Path.of(".");
108+
List<String> actual = new JavacTask(tb)
109+
.options("-XDrawDiagnostics",
110+
"-XDshould-stop.at=FLOW",
111+
"-XDdev")
112+
.sources(code)
113+
.outdir(curPath)
114+
.run(Expect.FAIL)
115+
.getOutputLines(OutputKind.DIRECT);
116+
117+
List<String> expected = List.of(
118+
"Test.java:2:19: compiler.err.abstract.meth.cant.have.body",
119+
"Test.java:4:13: compiler.err.cant.resolve.location.args: kindname.method, unresolvable, , , (compiler.misc.location: kindname.class, Test, null)",
120+
"2 errors"
121+
);
122+
123+
if (!Objects.equals(actual, expected)) {
124+
error("Expected: " + expected + ", but got: " + actual);
125+
}
126+
}
127+
128+
@Test
129+
public void testNativeMethodWithBody() throws Exception {
130+
String code = """
131+
class Test {
132+
native void test() {
133+
try {
134+
unresolvable();
135+
} catch (Throwable t) {
136+
throw new RuntimeException(t);
137+
}
138+
}
139+
}
140+
""";
141+
Path curPath = Path.of(".");
142+
List<String> actual = new JavacTask(tb)
143+
.options("-XDrawDiagnostics",
144+
"-XDshould-stop.at=FLOW",
145+
"-XDdev")
146+
.sources(code)
147+
.outdir(curPath)
148+
.run(Expect.FAIL)
149+
.getOutputLines(OutputKind.DIRECT);
150+
151+
List<String> expected = List.of(
152+
"Test.java:2:17: compiler.err.native.meth.cant.have.body",
153+
"Test.java:4:13: compiler.err.cant.resolve.location.args: kindname.method, unresolvable, , , (compiler.misc.location: kindname.class, Test, null)",
154+
"2 errors"
155+
);
156+
157+
if (!Objects.equals(actual, expected)) {
158+
error("Expected: " + expected + ", but got: " + actual);
159+
}
160+
}
161+
162+
}

0 commit comments

Comments
 (0)
Please sign in to comment.