Skip to content

Commit

Permalink
[JENKINS-39363,CID-1205051] - Prevent resource leak in hudson.XmlFile…
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
oleg-nenashev committed Oct 29, 2016
1 parent 1d16ce0 commit fe2b697
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion core/src/main/java/hudson/XmlFile.java
Expand Up @@ -51,6 +51,7 @@
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;

Expand Down Expand Up @@ -196,9 +197,19 @@ public String toString() {
* 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;
}
}

/**
Expand Down

0 comments on commit fe2b697

Please sign in to comment.