Hi everyone,

I'm afraid I've found a bug in commons.collection.buffer.UnboundedFifoBuffer
If someone else already spotted it, sorry for that : I didn't find it in the bug list.

When removing an object from an UnboundedFifoBuffer via its iterator method,
I can get an "ArrayIndexOutOfBoundException -1".

Here is the faulty code :

int i = lastReturnedIndex + 1;
while (i != tail) {
        if (i >= buffer.length) {
                buffer[i - 1] = buffer[0];
                i = 0;             // This will throw an exception at the next loop
        } else {
                buffer[i - 1] = buffer[i]; // The exception may be thrown here
                i++;
        }
}

If i >= buffer.length, then i is set to 0, and the next time in the loop, we will try 
to assign buffer[i - 1].
My solution :

int i = index;
while (i != tail) {
        if (i == 0) {
                buffer[buffer.length-1] = buffer[0];
                i = 1;
        } else {
                buffer[i - 1] = buffer[i];
                i = increment(i);
        }
}

Attached is a patch file...

Thanks
Luc

--- UnboundedFifoBuffer.java.orig       2004-11-05 14:16:57.000000000 +0100
+++ UnboundedFifoBuffer.java    2004-11-05 14:16:19.000000000 +0100
@@ -303,14 +303,14 @@
                 }
 
                 // Other elements require us to shift the subsequent elements
-                int i = lastReturnedIndex + 1;
+                int i = index;
                 while (i != tail) {
-                    if (i >= buffer.length) {
-                        buffer[i - 1] = buffer[0];
-                        i = 0;
+                    if (i == 0) {
+                        buffer[buffer.length - 1] = buffer[0];
+                        i = 1;
                     } else {
                         buffer[i - 1] = buffer[i];
-                        i++;
+                        i = increment(i);
                     }
                 }
 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to