mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Fix IndexOutOfBoundsException exception from parseUrl with empty spec
Update jar `Handler` code so that the `parseUrl` method can accept an empty `spec`. Prior to this commit, a `classLoader.getResource("")` call would result in a `null` result. This breaks a number of things including `ClassPathResource` and `PathMatchingResourcePatternResolver`. Fixes gh-38524
This commit is contained in:
parent
7387c1c64c
commit
0856e10443
@ -89,7 +89,7 @@ public class Handler extends URLStreamHandler {
|
|||||||
|
|
||||||
private String extractContextPath(URL url, String spec, int start) {
|
private String extractContextPath(URL url, String spec, int start) {
|
||||||
String contextPath = url.getPath();
|
String contextPath = url.getPath();
|
||||||
if (spec.charAt(start) == '/') {
|
if (spec.regionMatches(false, start, "/", 0, 1)) {
|
||||||
int indexOfContextPathSeparator = indexOfSeparator(contextPath);
|
int indexOfContextPathSeparator = indexOfSeparator(contextPath);
|
||||||
if (indexOfContextPathSeparator == -1) {
|
if (indexOfContextPathSeparator == -1) {
|
||||||
throw new IllegalStateException("malformed context url:%s: no !/".formatted(url));
|
throw new IllegalStateException("malformed context url:%s: no !/".formatted(url));
|
||||||
|
@ -142,6 +142,15 @@ class HandlerTests {
|
|||||||
assertThat(url.toExternalForm()).isEqualTo("jar:file:example.jar!/entry.txt#runtime");
|
assertThat(url.toExternalForm()).isEqualTo("jar:file:example.jar!/entry.txt#runtime");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // gh-38524
|
||||||
|
void parseUrlWhenSpecIsEmpty() throws MalformedURLException {
|
||||||
|
URL url = createJarUrl("nested:gh-38524.jar/!BOOT-INF/classes/!/");
|
||||||
|
String spec = "";
|
||||||
|
this.handler.parseURL(url, spec, 0, 0);
|
||||||
|
assertThat(url.toExternalForm()).isEqualTo("jar:nested:gh-38524.jar/!BOOT-INF/classes/!/");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void hashCodeGeneratesHashCode() throws MalformedURLException {
|
void hashCodeGeneratesHashCode() throws MalformedURLException {
|
||||||
URL url = createJarUrl("file:example.jar!/entry.txt");
|
URL url = createJarUrl("file:example.jar!/entry.txt");
|
||||||
|
Loading…
Reference in New Issue
Block a user