[LOG4J2-1541] Fix file handle resource leak in XmlConfiguration.XmlConfiguration(ConfigurationSource).
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/b7da5e73 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/b7da5e73 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/b7da5e73 Branch: refs/heads/LOG4J2-1349-gcfree-threadcontext Commit: b7da5e73cf30afd1ab130c5157fbb2bc59bbb90d Parents: de319af Author: Gary Gregory <ggreg...@apache.org> Authored: Mon Aug 22 19:18:14 2016 -0700 Committer: Gary Gregory <ggreg...@apache.org> Committed: Mon Aug 22 19:18:14 2016 -0700 ---------------------------------------------------------------------- .../log4j/core/config/xml/XmlConfiguration.java | 40 ++++++++++---------- src/changes/changes.xml | 3 ++ 2 files changed, 22 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b7da5e73/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java index ea3f753..799a49e 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java @@ -147,31 +147,29 @@ public class XmlConfiguration extends AbstractConfiguration implements Reconfigu LOGGER.error("Error parsing " + configSource.getLocation(), e); } if (strict && schemaResource != null && buffer != null) { - InputStream is = null; - try { - is = Loader.getResourceAsStream(schemaResource, XmlConfiguration.class.getClassLoader()); - } catch (final Exception ex) { - LOGGER.error("Unable to access schema {}", this.schemaResource, ex); - } - if (is != null) { - final Source src = new StreamSource(is, LOG4J_XSD); - final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); - Schema schema = null; - try { - schema = factory.newSchema(src); - } catch (final SAXException ex) { - LOGGER.error("Error parsing Log4j schema", ex); - } - if (schema != null) { - final Validator validator = schema.newValidator(); + try (InputStream is = Loader.getResourceAsStream(schemaResource, XmlConfiguration.class.getClassLoader())) { + if (is != null) { + final Source src = new StreamSource(is, LOG4J_XSD); + final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + Schema schema = null; try { - validator.validate(new StreamSource(new ByteArrayInputStream(buffer))); - } catch (final IOException ioe) { - LOGGER.error("Error reading configuration for validation", ioe); + schema = factory.newSchema(src); } catch (final SAXException ex) { - LOGGER.error("Error validating configuration", ex); + LOGGER.error("Error parsing Log4j schema", ex); + } + if (schema != null) { + final Validator validator = schema.newValidator(); + try { + validator.validate(new StreamSource(new ByteArrayInputStream(buffer))); + } catch (final IOException ioe) { + LOGGER.error("Error reading configuration for validation", ioe); + } catch (final SAXException ex) { + LOGGER.error("Error validating configuration", ex); + } } } + } catch (final Exception ex) { + LOGGER.error("Unable to access schema {}", this.schemaResource, ex); } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b7da5e73/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 93eeef0..f44d7ac 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -24,6 +24,9 @@ </properties> <body> <release version="2.7" date="2016-MM-DD" description="GA Release 2.7"> + <action issue="LOG4J2-1541" dev="ggregory" type="fix" due-to="Gary Gregory"> + Fix file handle resource leak in XmlConfiguration.XmlConfiguration(ConfigurationSource). + </action> <action issue="LOG4J2-1538" dev="ggregory" type="fix" due-to="Igor Karpov"> Dynamic removal of filter may cause NPE. </action>