Skip to content

Commit

Permalink
8263940: NPE when creating default file system when default file syst…
Browse files Browse the repository at this point in the history
…em provider is packaged as JAR file on class path

Reviewed-by: lucy
Backport-of: 717792c3b728584413572e7aede83290779be2a2
  • Loading branch information
Amos Shi committed Apr 25, 2024
1 parent 3324133 commit b13cb2f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/java.base/share/classes/java/util/zip/ZipFile.java
Expand Up @@ -33,7 +33,6 @@
import java.io.RandomAccessFile;
import java.io.UncheckedIOException;
import java.lang.ref.Cleaner.Cleanable;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.file.InvalidPathException;
import java.nio.file.attribute.BasicFileAttributes;
Expand Down Expand Up @@ -1419,14 +1418,19 @@ public boolean equals(Object obj) {
}
}
private static final HashMap<Key, Source> files = new HashMap<>();

/**
* Use the platform's default file system to avoid
* issues when the VM is configured to use a custom file system provider.
*/
private static final java.nio.file.FileSystem builtInFS =
DefaultFileSystemProvider.theFileSystem();

static Source get(File file, boolean toDelete, ZipCoder zc) throws IOException {
final Key key;
try {
key = new Key(file,
Files.readAttributes(file.toPath(), BasicFileAttributes.class),
zc);
Files.readAttributes(builtInFS.getPath(file.getPath()),
BasicFileAttributes.class), zc);
} catch (InvalidPathException ipe) {
throw new IOException(ipe);
}
Expand Down
46 changes: 44 additions & 2 deletions test/jdk/java/nio/file/spi/SetDefaultProvider.java
Expand Up @@ -23,7 +23,7 @@

/**
* @test
* @bug 8266345
* @bug 4313887 7006126 8142968 8178380 8183320 8210112 8266345 8263940
* @modules jdk.jartool
* @library /test/lib
* @build SetDefaultProvider TestProvider m/* jdk.test.lib.process.ProcessTools
Expand All @@ -37,11 +37,14 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.spi.ToolProvider;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import jdk.test.lib.process.ProcessTools;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.testng.Assert.*;

Expand Down Expand Up @@ -73,6 +76,45 @@ public void testClassPath() throws Exception {
assertTrue(exitValue == 0);
}

/**
* Test override of default FileSystemProvider with a
* FileSystemProvider jar and the main application on the class path.
*/
public void testClassPathWithFileSystemProviderJar() throws Exception {
String testClasses = System.getProperty("test.classes");
Path jar = Path.of("testFileSystemProvider.jar");
Files.deleteIfExists(jar);
createFileSystemProviderJar(jar, Path.of(testClasses));
String classpath = jar + File.pathSeparator + testClasses
+ File.separator + "modules" + File.separator + "m";
int exitValue = exec(SET_DEFAULT_FSP, "-cp", classpath, "p.Main");
assertTrue(exitValue == 0);
}

/**
* Creates a JAR containing the FileSystemProvider used to override the
* default FileSystemProvider
*/
private void createFileSystemProviderJar(Path jar, Path dir) throws IOException {

List<String> args = new ArrayList<>();
args.add("--create");
args.add("--file=" + jar);
try (Stream<Path> stream = Files.list(dir)) {
List<String> paths = stream
.map(path -> path.getFileName().toString())
.filter(f -> f.startsWith("TestProvider"))
.toList();
for(var p : paths) {
args.add("-C");
args.add(dir.toString());
args.add(p);
}
}
int ret = JAR_TOOL.run(System.out, System.out, args.toArray(new String[0]));
assertTrue(ret == 0);
}

/**
* Test override of default FileSystemProvider with the main application
* on the class path and a SecurityManager enabled.
Expand Down

1 comment on commit b13cb2f

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.