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

markt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new 6ee4600185 Align error handling for CoyoteWriter and CoyoteOutputStream
6ee4600185 is described below

commit 6ee4600185945c4c37ff81403c616bf9db3ec34f
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Feb 29 09:10:19 2024 +0000

    Align error handling for CoyoteWriter and CoyoteOutputStream
    
    Prior to this change it was possible for errors on the
    CoyoteOutputStream to be recorded against the wrong response if the
    response was recycled during the call to realWriteBytes().
---
 .../catalina/connector/CoyoteOutputStream.java     | 35 ++++++++++++++++++----
 .../apache/catalina/connector/CoyoteWriter.java    | 12 +++++---
 .../apache/catalina/connector/OutputBuffer.java    |  7 ++++-
 webapps/docs/changelog.xml                         |  7 +++++
 4 files changed, 51 insertions(+), 10 deletions(-)

diff --git a/java/org/apache/catalina/connector/CoyoteOutputStream.java 
b/java/org/apache/catalina/connector/CoyoteOutputStream.java
index c61eebf8e5..9dd3f11197 100644
--- a/java/org/apache/catalina/connector/CoyoteOutputStream.java
+++ b/java/org/apache/catalina/connector/CoyoteOutputStream.java
@@ -78,7 +78,12 @@ public class CoyoteOutputStream extends ServletOutputStream {
     @Override
     public void write(int i) throws IOException {
         boolean nonBlocking = checkNonBlockingWrite();
-        ob.writeByte(i);
+        try {
+            ob.writeByte(i);
+        } catch (IOException ioe) {
+            ob.setErrorException(ioe);
+            throw ioe;
+        }
         if (nonBlocking) {
             checkRegisterForWrite();
         }
@@ -94,7 +99,12 @@ public class CoyoteOutputStream extends ServletOutputStream {
     @Override
     public void write(byte[] b, int off, int len) throws IOException {
         boolean nonBlocking = checkNonBlockingWrite();
-        ob.write(b, off, len);
+        try {
+            ob.write(b, off, len);
+        } catch (IOException ioe) {
+            ob.setErrorException(ioe);
+            throw ioe;
+        }
         if (nonBlocking) {
             checkRegisterForWrite();
         }
@@ -108,7 +118,12 @@ public class CoyoteOutputStream extends 
ServletOutputStream {
         if (from.remaining() == 0) {
             return;
         }
-        ob.write(from);
+        try {
+            ob.write(from);
+        } catch (IOException ioe) {
+            ob.setErrorException(ioe);
+            throw ioe;
+        }
         if (nonBlocking) {
             checkRegisterForWrite();
         }
@@ -121,7 +136,12 @@ public class CoyoteOutputStream extends 
ServletOutputStream {
     @Override
     public void flush() throws IOException {
         boolean nonBlocking = checkNonBlockingWrite();
-        ob.flush();
+        try {
+            ob.flush();
+        } catch (IOException ioe) {
+            ob.setErrorException(ioe);
+            throw ioe;
+        }
         if (nonBlocking) {
             checkRegisterForWrite();
         }
@@ -156,7 +176,12 @@ public class CoyoteOutputStream extends 
ServletOutputStream {
 
     @Override
     public void close() throws IOException {
-        ob.close();
+        try {
+            ob.close();
+        } catch (IOException ioe) {
+            ob.setErrorException(ioe);
+            throw ioe;
+        }
     }
 
     @Override
diff --git a/java/org/apache/catalina/connector/CoyoteWriter.java 
b/java/org/apache/catalina/connector/CoyoteWriter.java
index 6eef35949d..6868156f0a 100644
--- a/java/org/apache/catalina/connector/CoyoteWriter.java
+++ b/java/org/apache/catalina/connector/CoyoteWriter.java
@@ -92,7 +92,7 @@ public class CoyoteWriter extends PrintWriter {
         try {
             ob.flush();
         } catch (IOException e) {
-            error = true;
+            setErrorException(e);
         }
 
     }
@@ -130,7 +130,7 @@ public class CoyoteWriter extends PrintWriter {
         try {
             ob.write(c);
         } catch (IOException e) {
-            error = true;
+            setErrorException(e);
         }
 
     }
@@ -146,7 +146,7 @@ public class CoyoteWriter extends PrintWriter {
         try {
             ob.write(buf, off, len);
         } catch (IOException e) {
-            error = true;
+            setErrorException(e);
         }
 
     }
@@ -168,7 +168,7 @@ public class CoyoteWriter extends PrintWriter {
         try {
             ob.write(s, off, len);
         } catch (IOException e) {
-            error = true;
+            setErrorException(e);
         }
 
     }
@@ -313,4 +313,8 @@ public class CoyoteWriter extends PrintWriter {
     }
 
 
+    private void setErrorException(Exception e) {
+        error = true;
+        ob.setErrorException(e);
+    }
 }
diff --git a/java/org/apache/catalina/connector/OutputBuffer.java 
b/java/org/apache/catalina/connector/OutputBuffer.java
index a0d85c4f64..f892ce3144 100644
--- a/java/org/apache/catalina/connector/OutputBuffer.java
+++ b/java/org/apache/catalina/connector/OutputBuffer.java
@@ -328,7 +328,6 @@ public class OutputBuffer extends Writer {
                 // An IOException on a write is almost always due to
                 // the remote client aborting the request. Wrap this
                 // so that it can be handled better by the error dispatcher.
-                coyoteResponse.setErrorException(e);
                 throw new ClientAbortException(e);
             }
         }
@@ -702,6 +701,12 @@ public class OutputBuffer extends Writer {
         }
     }
 
+
+    public void setErrorException(Exception e) {
+        coyoteResponse.setErrorException(e);
+    }
+
+
     private void appendByteArray(byte src[], int off, int len) throws 
IOException {
         if (len == 0) {
             return;
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index c7fcb3e46d..83ea4cb230 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -118,6 +118,13 @@
         Minor performance improvement for building filter chains. Based on
         ideas from <pr>702</pr> by Luke Miao. (remm)
       </fix>
+      <fix>
+        Align error handling for <code>Writer</code> and
+        <code>OutputStream</code>. Ensure use of either once the response has
+        been recycled triggers a <code>NullPointerException</code> provided 
that
+        <code>discardFacades</code> is configured with the default value of
+        <code>true</code>.
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to