Skip to content

Commit 1beee57

Browse files
committed
Don't close early when SecurityManager present
Update `JarFile` and `JarFileWrapper` classes so that they no longer close the `JarFile` early if a `SecurityManager` is in use. Prior to this commit, the closed `JarFile` would cause (an ultimately swallowed) NPE in `ZipFile` which manifested itself as a `ClassNotFoundException` when starting the app. Closes gh-25538
1 parent d0e2925 commit 1beee57

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -126,7 +126,9 @@ private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, RandomAccess
126126
private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, RandomAccessData data, JarEntryFilter filter,
127127
JarFileType type, Supplier<Manifest> manifestSupplier) throws IOException {
128128
super(rootFile.getFile());
129-
super.close();
129+
if (System.getSecurityManager() == null) {
130+
super.close();
131+
}
130132
this.rootFile = rootFile;
131133
this.pathFromRoot = pathFromRoot;
132134
CentralDirectoryParser parser = new CentralDirectoryParser();
@@ -137,7 +139,12 @@ private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, RandomAccess
137139
this.data = parser.parse(data, filter == null);
138140
}
139141
catch (RuntimeException ex) {
140-
close();
142+
try {
143+
this.rootFile.close();
144+
super.close();
145+
}
146+
catch (IOException ioex) {
147+
}
141148
throw ex;
142149
}
143150
this.manifestSupplier = (manifestSupplier != null) ? manifestSupplier : () -> {
@@ -337,10 +344,11 @@ public void close() throws IOException {
337344
if (this.closed) {
338345
return;
339346
}
340-
this.closed = true;
347+
super.close();
341348
if (this.type == JarFileType.DIRECT) {
342349
this.rootFile.close();
343350
}
351+
this.closed = true;
344352
}
345353

346354
private void ensureOpen() {

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFileWrapper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,7 +40,9 @@ class JarFileWrapper extends AbstractJarFile {
4040
JarFileWrapper(JarFile parent) throws IOException {
4141
super(parent.getRootJarFile().getFile());
4242
this.parent = parent;
43-
super.close();
43+
if (System.getSecurityManager() == null) {
44+
super.close();
45+
}
4446
}
4547

4648
@Override

0 commit comments

Comments
 (0)