8338445: jdk.internal.loader.URLClassPath may leak JarFile instance when dealing with unexpected Class-Path entry in manifest

Reviewed-by: michaelm, cstein, alanb
This commit is contained in:
Jaikiran Pai 2024-08-28 09:29:18 +00:00
parent 8e88da05b9
commit 2e174c6367
2 changed files with 180 additions and 11 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -59,7 +59,6 @@ import java.util.Properties;
import java.util.StringTokenizer;
import java.util.jar.JarFile;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.jar.JarEntry;
import java.util.jar.Manifest;
import java.util.jar.Attributes;
@ -439,27 +438,38 @@ public class URLClassPath {
continue;
}
// Otherwise, create a new Loader for the URL.
Loader loader;
Loader loader = null;
final URL[] loaderClassPathURLs;
try {
loader = getLoader(url);
// If the loader defines a local class path then add the
// URLs as the next URLs to be opened.
URL[] urls = loader.getClassPath();
if (urls != null) {
push(urls);
}
loaderClassPathURLs = loader.getClassPath();
} catch (IOException e) {
// Silently ignore for now...
// log the error and close the unusable loader (if any)
if (DEBUG) {
System.err.println("Failed to construct a loader or construct its" +
" local classpath for " + url + ", cause:" + e);
}
if (loader != null) {
closeQuietly(loader);
}
continue;
} catch (SecurityException se) {
// Always silently ignore. The context, if there is one, that
// this URLClassPath was given during construction will never
// have permission to access the URL.
// log the error and close the unusable loader (if any).
// The context, if there is one, that this URLClassPath was
// given during construction will never have permission to access the URL.
if (DEBUG) {
System.err.println("Failed to access " + url + ", " + se );
}
if (loader != null) {
closeQuietly(loader);
}
continue;
}
if (loaderClassPathURLs != null) {
push(loaderClassPathURLs);
}
// Finally, add the Loader to the search path.
loaders.add(loader);
lmap.put(urlNoFragString, loader);
@ -467,6 +477,17 @@ public class URLClassPath {
return loaders.get(index);
}
// closes the given loader and ignores any IOException that may occur during close
private static void closeQuietly(final Loader loader) {
try {
loader.close();
} catch (IOException ioe) {
if (DEBUG) {
System.err.println("ignoring exception " + ioe + " while closing loader " + loader);
}
}
}
/*
* Returns the Loader for the specified base URL.
*/