Skip to content

Commit c72c97c

Browse files
committed
Improve null-safety of loader/spring-boot-loader-tools
See spring-projectsgh-46587
1 parent b464334 commit c72c97c

File tree

6 files changed

+21
-7
lines changed

6 files changed

+21
-7
lines changed

loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/AbstractJarWriter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ public void writeLoaderClasses(@Nullable LoaderImplementation loaderImplementati
224224
@Override
225225
public void writeLoaderClasses(String loaderJarResourceName) throws IOException {
226226
URL loaderJar = getClass().getClassLoader().getResource(loaderJarResourceName);
227+
Assert.state(loaderJar != null, "Unable to load resource '%s'".formatted(loaderJarResourceName));
227228
try (JarInputStream inputStream = new JarInputStream(new BufferedInputStream(loaderJar.openStream()))) {
228229
JarEntry entry;
229230
while ((entry = inputStream.getNextJarEntry()) != null) {

loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
import org.jspecify.annotations.Nullable;
3333

34+
import org.springframework.util.Assert;
35+
3436
/**
3537
* Default implementation of {@link LaunchScript}. Provides the default Spring Boot launch
3638
* script or can load a specific script File. Also support mustache style template
@@ -63,7 +65,9 @@ public DefaultLaunchScript(@Nullable File file, Map<?, ?> properties) throws IOE
6365

6466
private String loadContent(@Nullable File file) throws IOException {
6567
if (file == null) {
66-
return loadContent(getClass().getResourceAsStream("launch.script"));
68+
InputStream stream = getClass().getResourceAsStream("launch.script");
69+
Assert.state(stream != null, "Unable to load resource 'launch.script'");
70+
return loadContent(stream);
6771
}
6872
return loadContent(new FileInputStream(file));
6973
}
@@ -85,7 +89,7 @@ private void copy(InputStream inputStream, OutputStream outputStream) throws IOE
8589
outputStream.flush();
8690
}
8791

88-
private String expandPlaceholders(@Nullable String content, Map<?, ?> properties) throws IOException {
92+
private String expandPlaceholders(String content, @Nullable Map<?, ?> properties) throws IOException {
8993
StringBuilder expanded = new StringBuilder();
9094
Matcher matcher = PLACEHOLDER_PATTERN.matcher(content);
9195
while (matcher.find()) {

loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/FileUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import org.jspecify.annotations.Nullable;
2727

28+
import org.springframework.util.Assert;
29+
2830
/**
2931
* Utilities for manipulating files and directories in Spring Boot tooling.
3032
*
@@ -43,7 +45,9 @@ public abstract class FileUtils {
4345
*/
4446
public static void removeDuplicatesFromOutputDirectory(File outputDirectory, File originDirectory) {
4547
if (originDirectory.isDirectory()) {
46-
for (String name : originDirectory.list()) {
48+
String[] files = originDirectory.list();
49+
Assert.state(files != null, "'files' must not be null");
50+
for (String name : files) {
4751
File targetFile = new File(outputDirectory, name);
4852
if (targetFile.exists() && targetFile.canWrite()) {
4953
if (!targetFile.isDirectory()) {

loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Library.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public String getName() {
109109
* @throws IOException on error
110110
*/
111111
InputStream openStream() throws IOException {
112+
Assert.state(this.file != null, "'file' must not be null");
112113
return new FileInputStream(this.file);
113114
}
114115

loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/MainClassFinder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ private static boolean isPackageDirectory(File file) {
154154
return null;
155155
}
156156

157-
private static void pushAllSorted(Deque<File> stack, File[] files) {
157+
private static void pushAllSorted(Deque<File> stack, File @Nullable [] files) {
158+
if (files == null) {
159+
return;
160+
}
158161
Arrays.sort(files, Comparator.comparing(File::getName));
159162
for (File file : files) {
160163
stack.push(file);

loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/SizeCalculatingEntryWriter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,18 @@ public void write(byte[] b, int off, int len) throws IOException {
115115
}
116116

117117
private OutputStream convertToFileOutputStream(ByteArrayOutputStream byteArrayOutputStream) throws IOException {
118-
initializeTempFile();
119-
FileOutputStream fileOutputStream = new FileOutputStream(this.tempFile);
118+
File tempFile = initializeTempFile();
119+
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
120120
StreamUtils.copy(byteArrayOutputStream.toByteArray(), fileOutputStream);
121121
return fileOutputStream;
122122
}
123123

124-
private void initializeTempFile() throws IOException {
124+
private File initializeTempFile() throws IOException {
125125
if (this.tempFile == null) {
126126
this.tempFile = File.createTempFile("springboot-", "-entrycontent");
127127
this.tempFile.deleteOnExit();
128128
}
129+
return this.tempFile;
129130
}
130131

131132
@Override

0 commit comments

Comments
 (0)