This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi-xmlbeans.git


The following commit(s) were added to refs/heads/trunk by this push:
     new fd2b423e Close the caller's stream when writeInputStreamToFile can't 
open the target (#114)
fd2b423e is described below

commit fd2b423ebe30281eb256f387d83b20e3b8bb1243
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Sep 7 13:01:08 2026 +0100

    Close the caller's stream when writeInputStreamToFile can't open the target 
(#114)
    
    SchemaResourceManager.writeInputStreamToFile relies on IOUtil.copyCompletely
    to close both streams, but copyCompletely only runs once the target has been
    opened. If Files.newOutputStream throws - an unwritable directory, a path
    component that is not a directory, a full disk - the caller's input is never
    closed.
    
    Callers in BaseSchemaResourceManager pass live downloads: url.openStream(),
    conn.getInputStream() and the index document stream. A write failure there
    leaks the socket.
    
    Own the input in a try-with-resources so it is released either way. The 
second
    close on the normal path is a no-op.
    
    Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
---
 .../xmlbeans/impl/tool/SchemaResourceManager.java  |  9 +++-
 .../impl/tool/SchemaResourceManagerWriteTest.java  | 61 ++++++++++++++++++++++
 2 files changed, 68 insertions(+), 2 deletions(-)

diff --git 
a/src/main/java/org/apache/xmlbeans/impl/tool/SchemaResourceManager.java 
b/src/main/java/org/apache/xmlbeans/impl/tool/SchemaResourceManager.java
index 17ac8f0c..54a1bb37 100644
--- a/src/main/java/org/apache/xmlbeans/impl/tool/SchemaResourceManager.java
+++ b/src/main/java/org/apache/xmlbeans/impl/tool/SchemaResourceManager.java
@@ -251,8 +251,13 @@ public class SchemaResourceManager extends 
BaseSchemaResourceManager
         File parent = targetFile.getParentFile();
         if (!parent.exists())
             parent.mkdirs();
-        OutputStream output = Files.newOutputStream(targetFile.toPath());
-        IOUtil.copyCompletely(input, output);
+        // copyCompletely() closes both streams, but it never runs if the 
target
+        // cannot be opened - and callers hand us a live download to close
+        try (InputStream in = input;
+             OutputStream output = Files.newOutputStream(targetFile.toPath()))
+        {
+            IOUtil.copyCompletely(in, output);
+        }
     }
 
     /**
diff --git 
a/src/test/java/org/apache/xmlbeans/impl/tool/SchemaResourceManagerWriteTest.java
 
b/src/test/java/org/apache/xmlbeans/impl/tool/SchemaResourceManagerWriteTest.java
new file mode 100644
index 00000000..a42d08b5
--- /dev/null
+++ 
b/src/test/java/org/apache/xmlbeans/impl/tool/SchemaResourceManagerWriteTest.java
@@ -0,0 +1,61 @@
+/*   Licensed to the Apache Software Foundation (ASF) under one or more
+ *   contributor license agreements.  See the NOTICE file distributed with
+ *   this work for additional information regarding copyright ownership.
+ *   The ASF licenses this file to You under the Apache License, Version 2.0
+ *   (the "License"); you may not use this file except in compliance with
+ *   the License.  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ */
+
+package org.apache.xmlbeans.impl.tool;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class SchemaResourceManagerWriteTest {
+
+    private static class TrackingInputStream extends ByteArrayInputStream {
+        private boolean closed;
+
+        TrackingInputStream(byte[] bytes) {
+            super(bytes);
+        }
+
+        @Override
+        public void close() throws IOException {
+            closed = true;
+            super.close();
+        }
+    }
+
+    @Test
+    void closesInputWhenTheTargetCannotBeOpened(@TempDir File dir) throws 
IOException {
+        // a plain file where the target's parent directory should be, so 
opening
+        // the output fails
+        File blocker = new File(dir, "blocker");
+        assertTrue(blocker.createNewFile());
+
+        SchemaResourceManager manager = new SchemaResourceManager(dir);
+        TrackingInputStream input =
+            new TrackingInputStream("<xsd/>".getBytes(StandardCharsets.UTF_8));
+
+        assertThrows(IOException.class,
+            () -> manager.writeInputStreamToFile(input, "blocker/child.xsd"));
+        assertTrue(input.closed, "the caller's stream should have been 
closed");
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to