changeset 2032:1b6fd3c61b05

8213734: SAXParser.parse(File, ..) does not close resources when Exception occurs. Reviewed-by: lancea
author joehw
date Fri, 30 Nov 2018 12:41:00 -0800
parents fd53cad7ff93
children a3dcb5ebdf43
files src/com/sun/org/apache/xerces/internal/impl/XMLEntityManager.java
diffstat 1 files changed, 19 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/sun/org/apache/xerces/internal/impl/XMLEntityManager.java	Mon Dec 16 11:27:38 2019 -0500
+++ b/src/com/sun/org/apache/xerces/internal/impl/XMLEntityManager.java	Fri Nov 30 12:41:00 2018 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
  */
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
@@ -824,7 +824,7 @@
 
         // We've seen a new Reader.
         // Push it on the stack so we can close it later.
-        //fOwnReaders.add(reader);
+        fReaderStack.push(reader);
 
         // push entity on stack
         if (fCurrentEntity != null) {
@@ -1347,16 +1347,21 @@
             (fEntityStack.empty() ? null : fEntityStack.elementAt(0));
     }
 
+    // A stack containing all the open readers
+    protected Stack<Reader> fReaderStack = new Stack<>();
 
     /**
      * Close all opened InputStreams and Readers opened by this parser.
      */
     public void closeReaders() {
-        /** this call actually does nothing, readers are closed in the endEntity method
-         * through the current entity.
-         * The change seems to have happened during the jdk6 development with the
-         * addition of StAX
-        **/
+        // close all readers
+        while (!fReaderStack.isEmpty()) {
+            try {
+                (fReaderStack.pop()).close();
+            } catch (IOException e) {
+                // ignore
+            }
+        }
     }
 
     public void endEntity() throws IOException, XNIException {
@@ -1390,6 +1395,13 @@
             }
         }
 
+        // REVISIT: We should never encounter underflow if the calls
+        // to startEntity and endEntity are balanced, but guard
+        // against the EmptyStackException for now. -- mrglavas
+        if (!fReaderStack.isEmpty()) {
+            fReaderStack.pop();
+        }
+
         if (fEntityHandler != null) {
             //so this is the last opened entity, signal it to current fEntityHandler using Augmentation
             if(entity == null){