Skip to content

Commit 217c2c8

Browse files
committed
Ignore file entries in META-INF/versions of multi-release jar
Fixes gh-41001
1 parent d4e9f45 commit 217c2c8

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -82,15 +82,17 @@ static MetaInfVersionsInfo get(int size, IntFunction<ZipContent.Entry> entries)
8282
if (contentEntry.hasNameStartingWith(META_INF_VERSIONS) && !contentEntry.isDirectory()) {
8383
String name = contentEntry.getName();
8484
int slash = name.indexOf('/', META_INF_VERSIONS.length());
85-
String version = name.substring(META_INF_VERSIONS.length(), slash);
86-
try {
87-
int versionNumber = Integer.parseInt(version);
88-
if (versionNumber >= NestedJarFile.BASE_VERSION) {
89-
versions.add(versionNumber);
85+
if (slash > -1) {
86+
String version = name.substring(META_INF_VERSIONS.length(), slash);
87+
try {
88+
int versionNumber = Integer.parseInt(version);
89+
if (versionNumber >= NestedJarFile.BASE_VERSION) {
90+
versions.add(versionNumber);
91+
}
92+
}
93+
catch (NumberFormatException ex) {
94+
// Ignore
9095
}
91-
}
92-
catch (NumberFormatException ex) {
93-
// Ignore
9496
}
9597
}
9698
}

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/MetaInfVersionsInfoTests.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -72,6 +72,20 @@ void getWhenHasNoEntriesReturnsNone() {
7272
assertThat(info).isSameAs(MetaInfVersionsInfo.NONE);
7373
}
7474

75+
@Test
76+
void toleratesUnexpectedFileEntryInMetaInfVersions() {
77+
List<ZipContent.Entry> entries = new ArrayList<>();
78+
entries.add(mockEntry("META-INF/"));
79+
entries.add(mockEntry("META-INF/MANIFEST.MF"));
80+
entries.add(mockEntry("META-INF/versions/"));
81+
entries.add(mockEntry("META-INF/versions/unexpected"));
82+
entries.add(mockEntry("META-INF/versions/9/"));
83+
entries.add(mockEntry("META-INF/versions/9/Foo.class"));
84+
MetaInfVersionsInfo info = MetaInfVersionsInfo.get(entries.size(), entries::get);
85+
assertThat(info.versions()).containsExactly(9);
86+
assertThat(info.directories()).containsExactly("META-INF/versions/9/");
87+
}
88+
7589
private ZipContent.Entry mockEntry(String name) {
7690
ZipContent.Entry entry = mock(ZipContent.Entry.class);
7791
given(entry.getName()).willReturn(name);

0 commit comments

Comments
 (0)