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

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new 6c6f3851e4 Guard LoggingPrintStream.forceFlush() against a closed 
stream (#13760)
6c6f3851e4 is described below

commit 6c6f3851e463778ddcc4fcd1372639d39932df44
Author: Arturo Bernal <[email protected]>
AuthorDate: Fri Oct 30 20:00:33 2026 +0100

    Guard LoggingPrintStream.forceFlush() against a closed stream (#13760)
    
    PrintStream#close() nulls out the inherited out field, so if something
    closes System.out/err before shutdown runs forceFlush() on it (e.g. a
    build tool invoking close() itself), the unguarded cast throws an NPE.
    Mirror the same instanceof guard the static forceFlush(PrintStream)
    helper already uses.
---
 .../apache/maven/logging/LoggingOutputStream.java  |  8 +++-
 .../maven/logging/LoggingOutputStreamTest.java     | 55 ++++++++++++++++++++++
 2 files changed, 62 insertions(+), 1 deletion(-)

diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/logging/LoggingOutputStream.java
 
b/impl/maven-core/src/main/java/org/apache/maven/logging/LoggingOutputStream.java
index e20546196f..473911fe12 100644
--- 
a/impl/maven-core/src/main/java/org/apache/maven/logging/LoggingOutputStream.java
+++ 
b/impl/maven-core/src/main/java/org/apache/maven/logging/LoggingOutputStream.java
@@ -85,7 +85,13 @@ public LoggingPrintStream(LoggingOutputStream out) {
         }
 
         public void forceFlush() {
-            ((LoggingOutputStream) out).forceFlush();
+            /*
+             * out is PrintStream's own field and is nulled out by 
PrintStream#close(), which can happen
+             * before this is called during shutdown (e.g. a build tool 
closing System.out/err itself).
+             */
+            if (out instanceof LoggingOutputStream los) {
+                los.forceFlush();
+            }
         }
     }
 
diff --git 
a/impl/maven-core/src/test/java/org/apache/maven/logging/LoggingOutputStreamTest.java
 
b/impl/maven-core/src/test/java/org/apache/maven/logging/LoggingOutputStreamTest.java
new file mode 100644
index 0000000000..e61e5166c2
--- /dev/null
+++ 
b/impl/maven-core/src/test/java/org/apache/maven/logging/LoggingOutputStreamTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.maven.logging;
+
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class LoggingOutputStreamTest {
+
+    @Test
+    void forceFlushAfterCloseDoesNotThrow() {
+        List<String> lines = new ArrayList<>();
+        LoggingOutputStream los = new LoggingOutputStream(lines::add);
+        PrintStream ps = los.printStream();
+        ps.print("unterminated");
+        ps.close();
+
+        assertDoesNotThrow(((LoggingOutputStream.LoggingPrintStream) 
ps)::forceFlush);
+        assertEquals(List.of(), lines);
+    }
+
+    @Test
+    void forceFlushBeforeCloseFlushesPendingLine() {
+        List<String> lines = new ArrayList<>();
+        LoggingOutputStream los = new LoggingOutputStream(lines::add);
+        PrintStream ps = los.printStream();
+        ps.print("unterminated");
+
+        ((LoggingOutputStream.LoggingPrintStream) ps).forceFlush();
+
+        assertEquals(List.of("unterminated"), lines);
+    }
+}

Reply via email to