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 717d69c  Fix BZ 65677 - improve error handling for HTTP reads with NIO2
717d69c is described below

commit 717d69c4808718341129e1c0669bf01af8cd7b43
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Nov 25 12:08:36 2021 +0000

    Fix BZ 65677 - improve error handling for HTTP reads with NIO2
    
    https://bz.apache.org/bugzilla/show_bug.cgi?id=65677
---
 java/org/apache/coyote/http11/Http11InputBuffer.java | 17 +++++++++++++++--
 webapps/docs/changelog.xml                           |  4 ++++
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/java/org/apache/coyote/http11/Http11InputBuffer.java 
b/java/org/apache/coyote/http11/Http11InputBuffer.java
index 19aa56e..fd9d18d 100644
--- a/java/org/apache/coyote/http11/Http11InputBuffer.java
+++ b/java/org/apache/coyote/http11/Http11InputBuffer.java
@@ -794,7 +794,7 @@ public class Http11InputBuffer implements InputBuffer, 
ApplicationBufferHandler
         }
 
         int nRead = -1;
-        byteBuffer.mark();
+        int mark = byteBuffer.position();
         try {
             if (byteBuffer.position() < byteBuffer.limit()) {
                 byteBuffer.position(byteBuffer.limit());
@@ -810,7 +810,20 @@ public class Http11InputBuffer implements InputBuffer, 
ApplicationBufferHandler
             // Ensure that the buffer limit and position are returned to a
             // consistent "ready for read" state if an error occurs during in
             // the above code block.
-            byteBuffer.limit(byteBuffer.position()).reset();
+            // Some error conditions can result in the position being reset to
+            // zero which also invalidates the mark.
+            // https://bz.apache.org/bugzilla/show_bug.cgi?id=65677
+            if (byteBuffer.position() >= mark) {
+                // // Position and mark are consistent. Assume a read (possibly
+                // of zero bytes) has occurred.
+                byteBuffer.limit(byteBuffer.position());
+                byteBuffer.position(mark);
+            } else {
+                // Position and mark are inconsistent. Set position and limit 
to
+                // zero so effectively no data is reported as read.
+                byteBuffer.position(0);
+                byteBuffer.limit(0);
+            }
         }
 
         if (log.isDebugEnabled()) {
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index e77ab39..fff859c 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -149,6 +149,10 @@
         Avoid unnecessary duplicate read registrations for blocking I/O with 
the
         NIO connector. (markt)
       </fix>
+      <fix>
+        <bug>65677</bug>: Improve exception handling for errors during HTTP/1.1
+        reads with NIO2. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="WebSocket">

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

Reply via email to