commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Knych, Thomas [IT]" <thomas.kn...@citigroup.com>
Subject RE: UnboundedFifoBuffer - serialization bug + fix
Date Fri, 15 Jul 2005 17:20:50 GMT
Dims -

The patch should be:

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
	in.defaultReadObject();
	int size = in.readInt();
-	buffer = new Object[size];     
+	buffer = new Object[size + 1];     
	for (int i = 0; i < size; i++) {
		buffer[i] = in.readObject();
	}
	head = 0;
	tail = size;
}



Setting tail = size - 1; just caused another bug (ie you couldn't get the last element in
the buffer). This is because tail is supposed to point to the next unused cell in the buffer
array. By creating the buffer to be size + 1, tail will be set to a valid cell within buffer
that is empty (but the cell before it has an element in it), and the class works properly.


Thanks,

-TK


-----Original Message-----
From: Davanum Srinivas [mailto:davanum@gmail.com]
Sent: Thursday, July 14, 2005 8:26 AM
To: Jakarta Commons Developers List
Cc: Knych, Thomas [IT]; *GT GFI ALF Development
Subject: Re: UnboundedFifoBuffer - serialization bug + fix


Jordan,

So the patch is the one-liner in previous email? ("tail = size - 1;"?)

-- dims

On 7/13/05, Krey, Jordan [IT] <jordan.krey@citigroup.com> wrote:
> Commons Collections-
> 
> Correction.
> 
> 116     /***
> 117      * Read the buffer in using a custom routine.
> 118      *
> 119      * @param in  the input stream
> 120      * @throws IOException
> 121      * @throws ClassNotFoundException
> 122      */
> 123     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
> 124         in.defaultReadObject();
> 125         int size = in.readInt();
> 126         buffer = new Object[size + 1];     <-- must be size + 1 in order to satisfy
the condition of Iterator.hasNext()
> 127         for (int i = 0; i < size; i++) {
> 128             buffer[i] = in.readObject();
> 129         }
> 130         head = 0;
> 131         tail = size;
> 132     }
> 
> For example, previously, if size == 1 and the object was read in.  The next iteration
would fail at the increment() and hasNext() methods:
> 246     private int increment(int index) {
> 247         index++;
> 248         if (index >= buffer.length) {       <-- buffer length == 1
> 249             index = 0;                              <-- index now set back to
0
> 250         }
> 251         return index;
> 252     }
> 
> 279             public boolean hasNext() {
> 280                 return index != tail;       <-- index == 0, tail == 1
> 281
> 282             }
> 
> Now:
> 246     private int increment(int index) {
> 247         index++;
> 248         if (index >= buffer.length) {       <-- buffer length == 2
> 249             index = 0;
> 250         }
> 251         return index;                               <-- index == 1
> 252     }
> 
> 279             public boolean hasNext() {
> 280                 return index != tail;       <-- index == 1, tail == 1
> 281
> 282             }
> 
> -Jordan & Citigroup Analytics Dev team
> 
> 
> -----Original Message-----
> From: Knych, Thomas [IT]
> Sent: Wednesday, July 13, 2005 10:15 AM
> To: 'commons-dev@jakarta.apache.org'
> Cc: *GT GFI ALF Development
> Subject: UnboundedFifoBuffer - serialization bug + fix
> 
> 
> Commons Collections-
> 
> We recently found a bug in the serialization of UnboundedFifoBuffer. Apparently when
the object is serialized and then read back in the tail variable is set to size; it should
actually be set to size - 1 because otherwise the tail element will be beyond the bounds of
the internal array. Among other things this will cause the iterator to infinitely loop.
> 
> 
> Attached is our own local patch...
> 
> Thanks!
> 
> -TK & Citigroup Analytics Dev team
> 
> 
> 
> 
> --- /home/tk53899/tmp/commons-colls/commons-collections-3.1/src/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java
  2005-07-13 10:08:12.000000000 -0400
> +++ org/apache/commons/collections/buffer/UnboundedFifoBuffer.java      2005-07-13 10:01:56.000000000
-0400
> @@ -50,7 +50,7 @@
>   * This class is Serializable from Commons Collections 3.1.
>   *
>   * @since Commons Collections 3.0 (previously in main package v2.1)
> - * @version $Revision: 1.9 $ $Date: 2004/06/01 22:57:18 $
> + * @version $Revision: 1.1.2.1 $ $Date: 2005/07/13 14:01:56 $
>   *
>   * @author Avalon
>   * @author Federico Barbieri
> @@ -128,7 +128,7 @@
>              buffer[i] = in.readObject();
>          }
>          head = 0;
> -        tail = size;
> +        tail = size - 1;
>      }
> 
>      //-----------------------------------------------------------------------
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 
> 


-- 
Davanum Srinivas -http://blogs.cocoondev.org/dims/

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message