8245696: javadoc crashes when a doc-files directory contains a '#' file

Reviewed-by: hannesw
This commit is contained in:
Jonathan Gibbons 2020-06-17 09:20:24 -07:00
parent ed4b801771
commit 2a794b696c
3 changed files with 64 additions and 1 deletions
src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets
formats/html
toolkit/resources
test/langtools/jdk/javadoc/doclet/testDocFiles

@ -49,6 +49,8 @@ import javax.lang.model.element.PackageElement;
import javax.tools.FileObject;
import javax.tools.JavaFileManager.Location;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -145,6 +147,15 @@ public class DocFilesHandlerImpl implements DocFilesHandler {
return;
}
for (DocFile srcfile: srcdir.list()) {
// ensure that the name is a valid component in an eventual full path
// and so avoid an equivalent check lower down in the file manager
// that throws IllegalArgumentException
if (!isValidFilename(srcfile)) {
configuration.messages.warning("doclet.Copy_Ignored_warning",
srcfile.getPath());
continue;
}
DocFile destfile = dstdir.resolve(srcfile.getName());
if (srcfile.isFile()) {
if (destfile.exists() && !first) {
@ -169,6 +180,16 @@ public class DocFilesHandlerImpl implements DocFilesHandler {
}
}
private boolean isValidFilename(DocFile f) {
try {
String n = f.getName();
URI u = new URI(n);
return u.getPath().equals(n);
} catch (URISyntaxException e) {
return false;
}
}
private void handleHtmlFile(DocFile srcfile, DocPath dstPath) throws DocFileIOException {
Utils utils = configuration.utils;
FileObject fileObject = srcfile.getFileObject();

@ -55,6 +55,7 @@ Please file a bug against the javadoc tool via the Java bug reporting page\n\
for duplicates. Include error messages and the following diagnostic in your report. Thank you.
doclet.File_not_found=File not found: {0}
doclet.Copy_Overwrite_warning=File {0} not copied to {1} due to existing file with same name...
doclet.Copy_Ignored_warning=File {0} not copied: invalid name
doclet.Copying_File_0_To_Dir_1=Copying file {0} to directory {1}...
doclet.Copying_File_0_To_File_1=Copying file {0} to file {1}...
doclet.No_Public_Classes_To_Document=No public or protected classes found to document.

@ -23,7 +23,7 @@
/*
* @test
* @bug 8008949 8234051
* @bug 8008949 8234051 8245696
* @summary doclet crashes if HTML files in module doc-files directories
* @library /tools/lib ../../lib
* @modules jdk.javadoc/jdk.javadoc.internal.tool
@ -33,6 +33,8 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import toolbox.ToolBox;
import javadoc.tester.JavadocTester;
@ -134,4 +136,43 @@ public class TestDocFiles extends JavadocTester {
checkOutput("m/p/doc-files/pkg-file.html", true,
"Package HTML file");
}
@Test
public void testBadFiles(Path base) throws IOException {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"package p; public class C { }");
Path df = src.resolve("p").resolve("doc-files");
// note that '?' may be an illegal filename character on some systems (e.g. Windows)
List<String> cases = List.of("valid", "#bad#", "#bad", "bad#bad", "bad?bad");
List<Path> files = new ArrayList<>();
for (String s : cases) {
try {
Path f = df.resolve(s);
tb.writeFile(f, "dummy contents");
files.add(f);
} catch (Throwable t) {
out.println("Cannot write doc-file " + s);
}
}
javadoc("-d", base.resolve("out").toString(),
"--source-path", src.toString(),
"p");
checkExit(Exit.OK);
// only check for those files
for (Path f : files) {
if (f.getFileName().toString().contains("valid")) {
checkOutput("p/doc-files/" + f.getFileName(), true,
"dummy contents");
} else {
// be careful handing file separator characters in the message
checkOutput(Output.OUT, true,
"warning - File " + f + " not copied: invalid name");
}
}
}
}