From 03abe9e943bd212bedd7ac032a43e061592c5f4d Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 7 Sep 2026 12:29:08 +0100 Subject: [PATCH] Close the caller's stream when writeInputStreamToFile can't open the target 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) --- .../impl/tool/SchemaResourceManager.java | 9 ++- .../tool/SchemaResourceManagerWriteTest.java | 61 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/apache/xmlbeans/impl/tool/SchemaResourceManagerWriteTest.java 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 17ac8f0c4..54a1bb375 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 @@ protected void writeInputStreamToFile(InputStream input, String filename) throws 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 000000000..a42d08b58 --- /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("".getBytes(StandardCharsets.UTF_8)); + + assertThrows(IOException.class, + () -> manager.writeInputStreamToFile(input, "blocker/child.xsd")); + assertTrue(input.closed, "the caller's stream should have been closed"); + } +}