8266291: (jrtfs) Calling Files.exists may break the JRT filesystem

Reviewed-by: redestad, alanb
This commit is contained in:
Athijegannathan Sundararajan 2021-05-14 12:59:41 +00:00
parent ebcf3991b7
commit af4cd04c2e
2 changed files with 35 additions and 4 deletions

View File

@ -468,6 +468,19 @@ public final class ImageReader implements AutoCloseable {
Node handleResource(String name) {
Node n = null;
if (!name.startsWith("/modules/")) {
return null;
}
// Make sure that the thing that follows "/modules/" is a module name.
int moduleEndIndex = name.indexOf('/', "/modules/".length());
if (moduleEndIndex == -1) {
return null;
}
ImageLocation moduleLoc = findLocation(name.substring(0, moduleEndIndex));
if (moduleLoc == null || moduleLoc.getModuleOffset() == 0) {
return null;
}
String locationPath = name.substring("/modules".length());
ImageLocation resourceLoc = findLocation(locationPath);
if (resourceLoc != null) {

View File

@ -244,22 +244,30 @@ public class Basic {
}
}
@DataProvider(name = "topLevelPkgDirs")
private Object[][] topLevelPkgDirs() {
@DataProvider(name = "topLevelNonExistingDirs")
private Object[][] topLevelNonExistingDirs() {
return new Object[][] {
{ "/java/lang" },
{ "java/lang" },
{ "/java/util" },
{ "java/util" },
{ "/modules/modules" },
{ "/modules/modules/" },
{ "/modules/modules/java.base" },
{ "/modules/modules/java.base/" },
{ "/modules/modules/java.base/java/lang/Object.class" },
{ "/modules/modules/javax.scripting" },
{ "/modules/modules/javax.scripting/" },
{ "/modules/modules/javax.scripting/javax/script/ScriptEngine.class" },
};
}
@Test(dataProvider = "topLevelPkgDirs")
@Test(dataProvider = "topLevelNonExistingDirs")
public void testNotExists(String path) throws Exception {
FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
Path dir = fs.getPath(path);
// package directories should not be there at top level
// These directories should not be there at top level
assertTrue(Files.notExists(dir));
}
@ -756,5 +764,15 @@ public class Basic {
assertTrue(Files.size(classFile) > 0L);
}
// @bug 8266291: (jrtfs) Calling Files.exists may break the JRT filesystem
@Test
public void fileExistsCallBreaksFileSystem() throws Exception {
Path p = FileSystems.getFileSystem(URI.create("jrt:/")).getPath("modules");
boolean wasDirectory = Files.isDirectory(p);
Path m = p.resolve("modules");
Files.exists(m);
assertTrue(wasDirectory == Files.isDirectory(p));
}
}