Skip to content

Commit 717792c

Browse files
author
Lance Andersen
committedAug 13, 2021
8263940: NPE when creating default file system when default file system provider is packaged as JAR file on class path
Reviewed-by: naoto, bpb, iris, joehw
1 parent d06d0b9 commit 717792c

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed
 

‎src/java.base/share/classes/java/util/zip/ZipFile.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.io.RandomAccessFile;
3434
import java.io.UncheckedIOException;
3535
import java.lang.ref.Cleaner.Cleanable;
36-
import java.nio.charset.CharacterCodingException;
3736
import java.nio.charset.Charset;
3837
import java.nio.file.InvalidPathException;
3938
import java.nio.file.attribute.BasicFileAttributes;
@@ -69,6 +68,7 @@
6968
import jdk.internal.ref.CleanerFactory;
7069
import jdk.internal.vm.annotation.Stable;
7170
import sun.nio.cs.UTF_8;
71+
import sun.nio.fs.DefaultFileSystemProvider;
7272
import sun.security.util.SignatureFileVerifier;
7373

7474
import static java.util.zip.ZipConstants64.*;
@@ -1255,14 +1255,19 @@ public boolean equals(Object obj) {
12551255
}
12561256
}
12571257
private static final HashMap<Key, Source> files = new HashMap<>();
1258-
1258+
/**
1259+
* Use the platform's default file system to avoid
1260+
* issues when the VM is configured to use a custom file system provider.
1261+
*/
1262+
private static final java.nio.file.FileSystem builtInFS =
1263+
DefaultFileSystemProvider.theFileSystem();
12591264

12601265
static Source get(File file, boolean toDelete, ZipCoder zc) throws IOException {
12611266
final Key key;
12621267
try {
12631268
key = new Key(file,
1264-
Files.readAttributes(file.toPath(), BasicFileAttributes.class),
1265-
zc);
1269+
Files.readAttributes(builtInFS.getPath(file.getPath()),
1270+
BasicFileAttributes.class), zc);
12661271
} catch (InvalidPathException ipe) {
12671272
throw new IOException(ipe);
12681273
}

‎test/jdk/java/nio/file/spi/SetDefaultProvider.java

+44-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* @test
26-
* @bug 8266345
26+
* @bug 4313887 7006126 8142968 8178380 8183320 8210112 8266345 8263940
2727
* @modules jdk.jartool
2828
* @library /test/lib
2929
* @build SetDefaultProvider TestProvider m/* jdk.test.lib.process.ProcessTools
@@ -37,11 +37,14 @@
3737
import java.nio.file.Files;
3838
import java.nio.file.Path;
3939
import java.nio.file.Paths;
40+
import java.util.ArrayList;
41+
import java.util.List;
4042
import java.util.spi.ToolProvider;
43+
import java.util.stream.Collectors;
44+
import java.util.stream.Stream;
4145

4246
import jdk.test.lib.process.ProcessTools;
4347

44-
import org.testng.annotations.BeforeTest;
4548
import org.testng.annotations.Test;
4649
import static org.testng.Assert.*;
4750

@@ -73,6 +76,45 @@ public void testClassPath() throws Exception {
7376
assertTrue(exitValue == 0);
7477
}
7578

79+
/**
80+
* Test override of default FileSystemProvider with a
81+
* FileSystemProvider jar and the main application on the class path.
82+
*/
83+
public void testClassPathWithFileSystemProviderJar() throws Exception {
84+
String testClasses = System.getProperty("test.classes");
85+
Path jar = Path.of("testFileSystemProvider.jar");
86+
Files.deleteIfExists(jar);
87+
createFileSystemProviderJar(jar, Path.of(testClasses));
88+
String classpath = jar + File.pathSeparator + testClasses
89+
+ File.separator + "modules" + File.separator + "m";
90+
int exitValue = exec(SET_DEFAULT_FSP, "-cp", classpath, "p.Main");
91+
assertTrue(exitValue == 0);
92+
}
93+
94+
/**
95+
* Creates a JAR containing the FileSystemProvider used to override the
96+
* default FileSystemProvider
97+
*/
98+
private void createFileSystemProviderJar(Path jar, Path dir) throws IOException {
99+
100+
List<String> args = new ArrayList<>();
101+
args.add("--create");
102+
args.add("--file=" + jar);
103+
try (Stream<Path> stream = Files.list(dir)) {
104+
List<String> paths = stream
105+
.map(path -> path.getFileName().toString())
106+
.filter(f -> f.startsWith("TestProvider"))
107+
.toList();
108+
for(var p : paths) {
109+
args.add("-C");
110+
args.add(dir.toString());
111+
args.add(p);
112+
}
113+
}
114+
int ret = JAR_TOOL.run(System.out, System.out, args.toArray(new String[0]));
115+
assertTrue(ret == 0);
116+
}
117+
76118
/**
77119
* Test override of default FileSystemProvider with the main application
78120
* on the class path and a SecurityManager enabled.

0 commit comments

Comments
 (0)
Please sign in to comment.