8065753: javac crashing on a html-like file
Summary: Avoiding special-case in error recovery for bad token on position 0.
Reviewed-by: jjg
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Thu Dec 04 19:09:29 2014 -0800
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Dec 08 11:50:19 2014 +0100
@@ -3067,7 +3067,7 @@
boolean checkForImports = true;
boolean firstTypeDecl = true;
while (token.kind != EOF) {
- if (token.pos > 0 && token.pos <= endPosTable.errorEndPos) {
+ if (token.pos <= endPosTable.errorEndPos) {
// error recovery
skip(checkForImports, false, false, false);
if (token.kind == EOF)
@@ -4083,7 +4083,7 @@
/**
* Store the last error position.
*/
- protected int errorEndPos;
+ protected int errorEndPos = Position.NOPOS;
public AbstractEndPosTable(JavacParser parser) {
this.parser = parser;
--- a/langtools/test/tools/javac/parser/JavacParserTest.java Thu Dec 04 19:09:29 2014 -0800
+++ b/langtools/test/tools/javac/parser/JavacParserTest.java Mon Dec 08 11:50:19 2014 +0100
@@ -23,7 +23,7 @@
/*
* @test
- * @bug 7073631 7159445 7156633 8028235
+ * @bug 7073631 7159445 7156633 8028235 8065753
* @summary tests error and diagnostics positions
* @author Jan Lahoda
*/
@@ -49,8 +49,10 @@
import com.sun.source.util.TreeScanner;
import com.sun.source.util.Trees;
import com.sun.tools.javac.api.JavacTaskImpl;
+import com.sun.tools.javac.main.Main;
import com.sun.tools.javac.tree.JCTree;
import java.io.IOException;
+import java.io.StringWriter;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -941,6 +943,19 @@
TypeKind.VOID);
}
+ @Test //JDK-8065753
+ void testWrongFirstToken() throws IOException {
+ String code = "<";
+ String expectedErrors = "Test.java:1:1: compiler.err.expected3: class, interface, enum\n" +
+ "1 error\n";
+ StringWriter out = new StringWriter();
+ JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(out, fm, null,
+ Arrays.asList("-XDrawDiagnostics"), null, Arrays.asList(new MyFileObject(code)));
+
+ assertEquals("the error code is not correct", Main.Result.ERROR, ct.doCall());
+ assertEquals("the error message is not correct", expectedErrors, out.toString());
+ }
+
void run(String[] args) throws Exception {
int passed = 0, failed = 0;
final Pattern p = (args != null && args.length > 0)