Skip to content
Permalink
Browse files
[FIXED JENKINS-37561, CID-1205051] - Prevent resource leak in AntClas…
…sLoader#findClassInComponents() (#2517)

It has been originally reported by Coverity in https://scan5.coverity.com/reports.htm#v36021/p10292/fileInstanceId=97573616&defectInstanceId=28155759&mergedDefectId=1205051. It happens on Exceptional paths only, but actually I see 31 runaway handlers on my jenkins-2.18 instance (maybe happens due to plugin dynamic load failure)

The issue happens, because finally block is misplaced. It handles only the last stream in this cycle
  • Loading branch information
oleg-nenashev committed Aug 20, 2016
1 parent 96c9786 commit 67347c3c5f097da968bf8e6fe9fb57b03e358677
Showing with 15 additions and 21 deletions.
  1. +15 −21 core/src/main/java/jenkins/util/AntClassLoader.java
@@ -1352,31 +1352,25 @@ private Class findClassInComponents(String name)
throws ClassNotFoundException {
// we need to search the components of the path to see if
// we can find the class we want.
InputStream stream = null;
String classFilename = getClassFilename(name);
try {
Enumeration e = pathComponents.elements();
while (e.hasMoreElements()) {
File pathComponent = (File) e.nextElement();
try {
stream = getResourceStream(pathComponent, classFilename);
if (stream != null) {
log("Loaded from " + pathComponent + " "
+ classFilename, Project.MSG_DEBUG);
return getClassFromStream(stream, name, pathComponent);
}
} catch (SecurityException se) {
throw se;
} catch (IOException ioe) {
// ioe.printStackTrace();
log("Exception reading component " + pathComponent + " (reason: "
+ ioe.getMessage() + ")", Project.MSG_VERBOSE);
Enumeration e = pathComponents.elements();
while (e.hasMoreElements()) {
File pathComponent = (File) e.nextElement();
try (final InputStream stream = getResourceStream(pathComponent, classFilename)) {
if (stream != null) {
log("Loaded from " + pathComponent + " "
+ classFilename, Project.MSG_DEBUG);
return getClassFromStream(stream, name, pathComponent);
}
} catch (SecurityException se) {
throw se;
} catch (IOException ioe) {
// ioe.printStackTrace();
log("Exception reading component " + pathComponent + " (reason: "
+ ioe.getMessage() + ")", Project.MSG_VERBOSE);
}
throw new ClassNotFoundException(name);
} finally {
FileUtils.close(stream);
}
throw new ClassNotFoundException(name);
}

/**

0 comments on commit 67347c3

Please sign in to comment.