From b13cb2f5db0ba178a1fcee54aa3209acdeb33e30 Mon Sep 17 00:00:00 2001 From: Amos Shi Date: Thu, 25 Apr 2024 15:14:10 +0000 Subject: [PATCH] 8263940: NPE when creating default file system when default file system provider is packaged as JAR file on class path Reviewed-by: lucy Backport-of: 717792c3b728584413572e7aede83290779be2a2 --- .../share/classes/java/util/zip/ZipFile.java | 12 +++-- .../java/nio/file/spi/SetDefaultProvider.java | 46 ++++++++++++++++++- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/java.base/share/classes/java/util/zip/ZipFile.java b/src/java.base/share/classes/java/util/zip/ZipFile.java index d10ad7f60a7..02946e885d6 100644 --- a/src/java.base/share/classes/java/util/zip/ZipFile.java +++ b/src/java.base/share/classes/java/util/zip/ZipFile.java @@ -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; @@ -1419,14 +1418,19 @@ public boolean equals(Object obj) { } } private static final HashMap 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); } diff --git a/test/jdk/java/nio/file/spi/SetDefaultProvider.java b/test/jdk/java/nio/file/spi/SetDefaultProvider.java index 29953c33e8e..0c012b24bf8 100644 --- a/test/jdk/java/nio/file/spi/SetDefaultProvider.java +++ b/test/jdk/java/nio/file/spi/SetDefaultProvider.java @@ -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 @@ -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.*; @@ -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 args = new ArrayList<>(); + args.add("--create"); + args.add("--file=" + jar); + try (Stream stream = Files.list(dir)) { + List 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.