This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch: FYI: bug fix in handling of chunked streams


I'm checking this in on the 4.1 branch.  I'll also put it in Classpath
shortly.

This bug is shown in the test case here:

    https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=178703

The problem turns out to be that in some cases our chunked input
stream can return 0.  This should only happen in situations where the
requested read length is 0, but in this code it can occur in other
cases.  It turns out that the calculation for how many bytes should be
read is wrong.

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>

	* gnu/java/net/protocol/http/ChunkedInputStream.java (read):
	Fixed calculation of number of bytes to read.
	(size, count, meta, eof): Document.

Index: gnu/java/net/protocol/http/ChunkedInputStream.java
===================================================================
--- gnu/java/net/protocol/http/ChunkedInputStream.java	(revision 110177)
+++ gnu/java/net/protocol/http/ChunkedInputStream.java	(working copy)
@@ -1,5 +1,5 @@
 /* ChunkedInputStream.java --
-   Copyright (C) 2004 Free Software Foundation, Inc.
+   Copyright (C) 2004, 2006 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -55,9 +55,16 @@
   private static final byte CR = 0x0d;
   private static final byte LF = 0x0a;
 
+  /** Size of the chunk we're reading.  */
   int size;
+  /** Number of bytes we've read in this chunk.  */
   int count;
+  /**
+   * True when we should read meta-information, false when we should
+   * read data.
+   */
   boolean meta;
+  /** True when we've hit EOF.  */
   boolean eof;
   Headers headers;
 
@@ -142,17 +149,22 @@
       }
     else
       {
-        int diff = length - offset;
-        int max = size - count;
-        max = (diff < max) ? diff : max;
-        int len = (max > 0) ? in.read(buffer, offset, max) : 0;
+	int canRead = Math.min(size - count, length);
+	int len = in.read(buffer, offset, canRead);
+	if (len == -1)
+	  {
+	    // This is an error condition but it isn't clear what we
+	    // should do with it.
+	    eof = true;
+	    return -1;
+	  }
         count += len;
         if (count == size)
           {
             // Read CRLF
             int c1 = in.read();
             int c2 = in.read();
-            if (c1 == -1 && c2 == -1)
+            if (c1 == -1 || c2 == -1)
               {
                 // EOF before CRLF: bad, but ignore
                 eof = true;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]