Skip to content

Commit af4cd04

Browse files
committed
8266291: (jrtfs) Calling Files.exists may break the JRT filesystem
Reviewed-by: redestad, alanb
1 parent ebcf399 commit af4cd04

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/java.base/share/classes/jdk/internal/jimage/ImageReader.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,19 @@ Node handleModulesSubTree(String name, ImageLocation loc) {
468468

469469
Node handleResource(String name) {
470470
Node n = null;
471+
if (!name.startsWith("/modules/")) {
472+
return null;
473+
}
474+
// Make sure that the thing that follows "/modules/" is a module name.
475+
int moduleEndIndex = name.indexOf('/', "/modules/".length());
476+
if (moduleEndIndex == -1) {
477+
return null;
478+
}
479+
ImageLocation moduleLoc = findLocation(name.substring(0, moduleEndIndex));
480+
if (moduleLoc == null || moduleLoc.getModuleOffset() == 0) {
481+
return null;
482+
}
483+
471484
String locationPath = name.substring("/modules".length());
472485
ImageLocation resourceLoc = findLocation(locationPath);
473486
if (resourceLoc != null) {

test/jdk/jdk/internal/jrtfs/Basic.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,30 @@ public void testKnownDirectories(String path, boolean theDefault) throws Excepti
244244
}
245245
}
246246

247-
@DataProvider(name = "topLevelPkgDirs")
248-
private Object[][] topLevelPkgDirs() {
247+
@DataProvider(name = "topLevelNonExistingDirs")
248+
private Object[][] topLevelNonExistingDirs() {
249249
return new Object[][] {
250250
{ "/java/lang" },
251251
{ "java/lang" },
252252
{ "/java/util" },
253253
{ "java/util" },
254+
{ "/modules/modules" },
255+
{ "/modules/modules/" },
256+
{ "/modules/modules/java.base" },
257+
{ "/modules/modules/java.base/" },
258+
{ "/modules/modules/java.base/java/lang/Object.class" },
259+
{ "/modules/modules/javax.scripting" },
260+
{ "/modules/modules/javax.scripting/" },
261+
{ "/modules/modules/javax.scripting/javax/script/ScriptEngine.class" },
254262
};
255263
}
256264

257-
@Test(dataProvider = "topLevelPkgDirs")
265+
@Test(dataProvider = "topLevelNonExistingDirs")
258266
public void testNotExists(String path) throws Exception {
259267
FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
260268
Path dir = fs.getPath(path);
261269

262-
// package directories should not be there at top level
270+
// These directories should not be there at top level
263271
assertTrue(Files.notExists(dir));
264272
}
265273

@@ -756,5 +764,15 @@ public void objectClassSizeTest() throws Exception {
756764

757765
assertTrue(Files.size(classFile) > 0L);
758766
}
767+
768+
// @bug 8266291: (jrtfs) Calling Files.exists may break the JRT filesystem
769+
@Test
770+
public void fileExistsCallBreaksFileSystem() throws Exception {
771+
Path p = FileSystems.getFileSystem(URI.create("jrt:/")).getPath("modules");
772+
boolean wasDirectory = Files.isDirectory(p);
773+
Path m = p.resolve("modules");
774+
Files.exists(m);
775+
assertTrue(wasDirectory == Files.isDirectory(p));
776+
}
759777
}
760778

0 commit comments

Comments
 (0)