Skip to content

Commit 9d8e3d4

Browse files
committed
Explicit check for duplicates in addClassPathManifestEntries
Issue: SPR-15989
1 parent 95b83fe commit 9d8e3d4

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ protected void addAllClassLoaderJarRoots(@Nullable ClassLoader classLoader, Set<
371371
for (URL url : ((URLClassLoader) classLoader).getURLs()) {
372372
try {
373373
UrlResource jarResource = new UrlResource(
374-
ResourceUtils.JAR_URL_PREFIX + url.toString() + ResourceUtils.JAR_URL_SEPARATOR);
374+
ResourceUtils.JAR_URL_PREFIX + url + ResourceUtils.JAR_URL_SEPARATOR);
375375
if (jarResource.exists()) {
376376
result.add(jarResource);
377377
}
@@ -423,11 +423,11 @@ protected void addClassPathManifestEntries(Set<Resource> result) {
423423
for (String path : StringUtils.delimitedListToStringArray(
424424
javaClassPathProperty, System.getProperty("path.separator"))) {
425425
try {
426-
File file = new File(path);
426+
String filePath = new File(path).getAbsolutePath();
427427
UrlResource jarResource = new UrlResource(ResourceUtils.JAR_URL_PREFIX +
428-
ResourceUtils.FILE_URL_PREFIX + file.getAbsolutePath() +
429-
ResourceUtils.JAR_URL_SEPARATOR);
430-
if (jarResource.exists()) {
428+
ResourceUtils.FILE_URL_PREFIX + filePath + ResourceUtils.JAR_URL_SEPARATOR);
429+
// Potentially overlapping with URLClassLoader.getURLs() result above!
430+
if (!result.contains(jarResource) && !hasDuplicate(filePath, result) && jarResource.exists()) {
431431
result.add(jarResource);
432432
}
433433
}
@@ -446,6 +446,29 @@ protected void addClassPathManifestEntries(Set<Resource> result) {
446446
}
447447
}
448448

449+
/**
450+
* Check whether the given file path has a duplicate but differently structured entry
451+
* in the existing result, i.e. with or without a leading slash.
452+
* @param filePath the file path (with or without a leading slash)
453+
* @param result the current result
454+
* @return {@code true} if there is a duplicate (i.e. to ignore the given file path),
455+
* {@code false} to proceed with adding a corresponding resource to the current result
456+
*/
457+
private boolean hasDuplicate(String filePath, Set<Resource> result) {
458+
if (result.isEmpty()) {
459+
return false;
460+
}
461+
String duplicatePath = (filePath.startsWith("/") ? filePath.substring(1) : "/" + filePath);
462+
try {
463+
return result.contains(new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX +
464+
duplicatePath + ResourceUtils.JAR_URL_SEPARATOR));
465+
}
466+
catch (MalformedURLException ex) {
467+
// Ignore: just for testing against duplicate.
468+
return false;
469+
}
470+
}
471+
449472
/**
450473
* Find all resources that match the given location pattern via the
451474
* Ant-style PathMatcher. Supports resources in jar files and zip files

0 commit comments

Comments
 (0)