Skip to content

Commit 7cb54de

Browse files
committed
Apply root dir cleanup only for GraalVM native image resource scheme
See gh-29163
1 parent 7a15805 commit 7cb54de

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -738,16 +738,16 @@ protected JarFile getJarFile(String jarFileUrl) throws IOException {
738738
protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern)
739739
throws IOException {
740740

741-
742741
URI rootDirUri = rootDirResource.getURI();
743-
String rootDir = rootDirUri.getRawPath();
744-
if (!"file".equals(rootDirUri.getScheme()) && rootDir.startsWith("/")) {
745-
rootDir = stripLeadingSlash(rootDir);
746-
rootDir = stripTrailingSlash(rootDir);
747-
if (rootDir.isEmpty()) {
748-
rootDir = "/";
749-
}
742+
String rootDir = rootDirUri.getPath();
743+
// If the URI is for a "resource" in the GraalVM native image file system, we have to
744+
// ensure that the root directory does not end in a slash while simultaneously ensuring
745+
// that the root directory is not an empty string (since fileSystem.getPath("").resolve(str)
746+
// throws an ArrayIndexOutOfBoundsException in a native image).
747+
if ("resource".equals(rootDirUri.getScheme()) && (rootDir.length() > 1) && rootDir.endsWith("/")) {
748+
rootDir = rootDir.substring(0, rootDir.length() - 1);
750749
}
750+
751751
FileSystem fileSystem;
752752
try {
753753
fileSystem = FileSystems.getFileSystem(rootDirUri.resolve("/"));
@@ -873,10 +873,6 @@ private static String stripLeadingSlash(String path) {
873873
return (path.startsWith("/") ? path.substring(1) : path);
874874
}
875875

876-
private static String stripTrailingSlash(String path) {
877-
return (path.endsWith("/") ? path.substring(0, path.length() - 1) : path);
878-
}
879-
880876

881877
/**
882878
* Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime.

0 commit comments

Comments
 (0)