[jdom-interest] Bug in ContentList.addAll(int,Collection)

Rolf Lear rlear at algorithmics.com
Thu Apr 17 05:52:28 PDT 2003


Hi all. In my performance perusal, I have identified a probable bug in the
above method.

The method has the following code:

        int count = 0;
        try {
            Iterator i = collection.iterator();
            while (i.hasNext()) {
                Object obj = i.next();
                add(index + count, obj);
                count++;
            }
        }
        catch (RuntimeException exception) {
            for (int i = 0; i < count; i++) {
                remove(index + i);
            }
            throw exception;
        }

Which implies that IF there was a problem during the add, the added elements
will be removed.

Unfortunately, the remove(index + i) will do the wrong thing.

Basically, the remove method already shifts all the subsequent data "left",
so, for example,

if 2 elements (Element A, B, and C) are added in the addAll, at index 4 in
the element list abcdefg to get the desired list:
abcdABCefg
but element C failed, then the "count" will be 2, and the lsit will actually
be:
abcdEBefg
The first iteration of the remove will make the list:
abcdBefg
and the second will make it:
abcdBgf.

The remove lines should be:
            for (int i = 0; i < count; i++) {
                remove(index);
            }


Rolf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20030417/6ec04801/attachment.htm


More information about the jdom-interest mailing list