Skip to content

Commit

Permalink
Prevent StackOverflow when parsing working copy of module declaration
Browse files Browse the repository at this point in the history
- Also prevent ClassCastException uncovered by fixing the issue

Fixes #2280

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 authored and jukzi committed Apr 5, 2024
1 parent 7000a5c commit 61fbe37
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ public void buildTypeBindings(CompilationUnitDeclaration unit, AccessRestriction
scope = new CompilationUnitScope(unit, this.globalOptions);
unitModule = unit.moduleDeclaration.setBinding(new SourceModuleBinding(moduleName, scope, this.root));
} else {
if (this.globalOptions.sourceLevel >= ClassFileConstants.JDK9) {
if (this.globalOptions.sourceLevel >= ClassFileConstants.JDK9 && !unit.isModuleInfo()) {
unitModule = unit.module(this);
}
scope = new CompilationUnitScope(unit, unitModule != null ? unitModule.environment : this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,23 @@
*******************************************************************************/
package org.eclipse.jdt.core.tests.dom;

import junit.framework.Test;

import org.eclipse.jdt.core.dom.*;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.core.JrtPackageFragmentRoot;
import org.eclipse.jdt.internal.core.SourceModule;

import java.util.List;
import java.util.function.Consumer;

import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IModularClassFile;
import org.eclipse.jdt.core.IModuleDescription;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.*;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.*;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.core.JrtPackageFragmentRoot;
import org.eclipse.jdt.internal.core.SourceModule;
import org.eclipse.text.edits.ReplaceEdit;

import junit.framework.Test;

@SuppressWarnings({"rawtypes"})
public class ASTConverter9Test extends ConverterTestSetup {
Expand Down Expand Up @@ -1475,5 +1467,52 @@ public void testBug542795() throws Exception {
deleteProject(p);
}
}
public void testStackOverflowInEmptiedModuleDeclarationParsing() throws JavaModelException, CoreException {
try {
IJavaProject project1 = createJavaProject("ASTParserModelTests", new String[] { "src" },
new String[] { "CONVERTER_JCL9_LIB" }, "bin", "9");
project1.open(null);
addClasspathEntry(project1, JavaCore.newContainerEntry(new Path("org.eclipse.jdt.MODULE_PATH")));
String content = """
module first {
requires transitive static second.third;
exports pack1.X11 to org.eclipse.jdt;
}
""";
createFile("/ASTParserModelTests/src/module-info.java", content);
this.workingCopy = getCompilationUnit("/ASTParserModelTests/src/module-info.java");
this.workingCopy.getBuffer().setContents("");

ASTParser astParser = ASTParser.newParser(AST.getJLSLatest());
astParser.setSource(this.workingCopy);
astParser.setResolveBindings(true);
astParser.setStatementsRecovery(true);
ASTNode astNode = astParser.createAST(new NullProgressMonitor());
assertEquals("", astNode.toString());
} finally {
deleteProject("ASTParserModelTests");
}
}
public void testClassCastExceptionWhenOpeningModuleInfoWithClass() throws JavaModelException, CoreException {
try {
IJavaProject project1 = createJavaProject("ASTParserModelTests", new String[] { "src" },
new String[] { "CONVERTER_JCL9_LIB" }, "bin", "9");
project1.open(null);
addClasspathEntry(project1, JavaCore.newContainerEntry(new Path("org.eclipse.jdt.MODULE_PATH")));
String content = """
module first {
}
""";
createFile("/ASTParserModelTests/src/module-info.java", content);
this.workingCopy = getCompilationUnit("/ASTParserModelTests/src/module-info.java");
this.workingCopy.becomeWorkingCopy(null);
this.workingCopy.applyTextEdit(new ReplaceEdit(0, 6, "class"), null);
this.workingCopy.reconcile(AST.getJLSLatest(), true, false, null, null);
project1.getProject().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, null);
assertEquals(0, this.workingCopy.getChildren().length);
} finally {
deleteProject("ASTParserModelTests");
}
}
// Add new tests here
}
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public void notifySourceElementRequestor(
} else {
notifySourceElementRequestor(importRef, false);
}
} else if (node instanceof TypeDeclaration) {
} else if (node instanceof TypeDeclaration && !new String(parsedUnit.getFileName()).endsWith(TypeConstants.MODULE_INFO_FILE_NAME_STRING)) {
notifySourceElementRequestor((TypeDeclaration)node, true, null, currentPackage);
} else if (node instanceof ModuleDeclaration) {
notifySourceElementRequestor(parsedUnit.moduleDeclaration);
Expand Down

0 comments on commit 61fbe37

Please sign in to comment.