From fe2b697e145a5d1c2b5cc60fc5f63b18b0aa79f2 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Sat, 29 Oct 2016 15:28:39 +0400 Subject: [PATCH] [JENKINS-39363,CID-1205051] - Prevent resource leak in hudson.XmlFile#readRaw() in the case of encoding issues. (#2518) [JENKINS-39363,CID-1205051] - Prevent resource leak in hudson.XmlFile#readRaw() in the case of encoding issues. --- core/src/main/java/hudson/XmlFile.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/XmlFile.java b/core/src/main/java/hudson/XmlFile.java index 933621b74b..9f438eeded 100644 --- a/core/src/main/java/hudson/XmlFile.java +++ b/core/src/main/java/hudson/XmlFile.java @@ -51,6 +51,7 @@ import java.io.InputStreamReader; import java.io.Reader; import java.io.Writer; import java.io.StringWriter; +import java.io.UnsupportedEncodingException; import java.util.logging.Level; import java.util.logging.Logger; @@ -196,9 +197,19 @@ public final class XmlFile { * Opens a {@link Reader} that loads XML. * This method uses {@link #sniffEncoding() the right encoding}, * not just the system default encoding. + * @throws IOException Encoding issues + * @return Reader for the file. should be close externally once read. */ public Reader readRaw() throws IOException { - return new InputStreamReader(new FileInputStream(file),sniffEncoding()); + FileInputStream fileInputStream = new FileInputStream(file); + try { + return new InputStreamReader(fileInputStream, sniffEncoding()); + } catch(IOException ex) { + // Exception may happen if we fail to find encoding or if this encoding is unsupported. + // In such case we close the underlying stream and rethrow. + Util.closeAndLogFailures(fileInputStream, LOGGER, "FileInputStream", file.toString()); + throw ex; + } } /**