diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/MetaInfVersionsInfo.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/MetaInfVersionsInfo.java index caf76a2b96f..87576693f5e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/MetaInfVersionsInfo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/MetaInfVersionsInfo.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -82,15 +82,17 @@ final class MetaInfVersionsInfo { if (contentEntry.hasNameStartingWith(META_INF_VERSIONS) && !contentEntry.isDirectory()) { String name = contentEntry.getName(); int slash = name.indexOf('/', META_INF_VERSIONS.length()); - String version = name.substring(META_INF_VERSIONS.length(), slash); - try { - int versionNumber = Integer.parseInt(version); - if (versionNumber >= NestedJarFile.BASE_VERSION) { - versions.add(versionNumber); + if (slash > -1) { + String version = name.substring(META_INF_VERSIONS.length(), slash); + try { + int versionNumber = Integer.parseInt(version); + if (versionNumber >= NestedJarFile.BASE_VERSION) { + versions.add(versionNumber); + } + } + catch (NumberFormatException ex) { + // Ignore } - } - catch (NumberFormatException ex) { - // Ignore } } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/MetaInfVersionsInfoTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/MetaInfVersionsInfoTests.java index d556c9cbea5..0c0341fe0fc 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/MetaInfVersionsInfoTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/MetaInfVersionsInfoTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,6 +72,20 @@ class MetaInfVersionsInfoTests { assertThat(info).isSameAs(MetaInfVersionsInfo.NONE); } + @Test + void toleratesUnexpectedFileEntryInMetaInfVersions() { + List entries = new ArrayList<>(); + entries.add(mockEntry("META-INF/")); + entries.add(mockEntry("META-INF/MANIFEST.MF")); + entries.add(mockEntry("META-INF/versions/")); + entries.add(mockEntry("META-INF/versions/unexpected")); + entries.add(mockEntry("META-INF/versions/9/")); + entries.add(mockEntry("META-INF/versions/9/Foo.class")); + MetaInfVersionsInfo info = MetaInfVersionsInfo.get(entries.size(), entries::get); + assertThat(info.versions()).containsExactly(9); + assertThat(info.directories()).containsExactly("META-INF/versions/9/"); + } + private ZipContent.Entry mockEntry(String name) { ZipContent.Entry entry = mock(ZipContent.Entry.class); given(entry.getName()).willReturn(name);