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

Bradley S. Huffman hip at a.cs.okstate.edu
Thu Apr 17 06:47:01 PDT 2003


Sure enough that's a bug.  Bet it's also in AttributeList.

Brad

Rolf Lear writes:

> This message is in MIME format. Since your mail reader does not understand
> this format, some or all of this message may not be legible.
> 
> ------_=_NextPart_001_01C304E0.3396236E
> Content-Type: text/plain;
> 	charset="iso-8859-1"
> 
> 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
> 
> ------_=_NextPart_001_01C304E0.3396236E
> Content-Type: text/html;
> 	charset="iso-8859-1"
> Content-Transfer-Encoding: quoted-printable
> 
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
> <HTML>
> <HEAD>
> <META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
> charset=3Diso-8859-1">
> <META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
> 5.5.2655.35">
> <TITLE>Bug in ContentList.addAll(int,Collection)</TITLE>
> </HEAD>
> <BODY>
> 
> <P><FONT SIZE=3D2>Hi all. In my performance perusal, I have identified =
> a probable bug in the above method.</FONT>
> </P>
> 
> <P><FONT SIZE=3D2>The method has the following code:</FONT>
> </P>
> 
> <P><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int count =
> =3D 0;</FONT>
> <BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try =
> {</FONT>
> <BR><FONT =
> SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
> sp; Iterator i =3D collection.iterator();</FONT>
> <BR><FONT =
> SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
> sp; while (i.hasNext()) {</FONT>
> <BR><FONT =
> SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
> sp;&nbsp;&nbsp;&nbsp;&nbsp; Object obj =3D i.next();</FONT>
> <BR><FONT =
> SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
> sp;&nbsp;&nbsp;&nbsp;&nbsp; add(index + count, obj);</FONT>
> <BR><FONT =
> SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
> sp;&nbsp;&nbsp;&nbsp;&nbsp; count++;</FONT>
> <BR><FONT =
> SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
> sp; }</FONT>
> <BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
> <BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch =
> (RuntimeException exception) {</FONT>
> <BR><FONT =
> SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
> sp; for (int i =3D 0; i &lt; count; i++) {</FONT>
> <BR><FONT =
> SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
> sp;&nbsp;&nbsp;&nbsp;&nbsp; remove(index + i);</FONT>
> <BR><FONT =
> SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
> sp; }</FONT>
> <BR><FONT =
> SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
> sp; throw exception;</FONT>
> <BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
> </P>
> 
> <P><FONT SIZE=3D2>Which implies that IF there was a problem during the =
> add, the added elements will be removed.</FONT>
> </P>
> 
> <P><FONT SIZE=3D2>Unfortunately, the remove(index + i) will do the =
> wrong thing.</FONT>
> </P>
> 
> <P><FONT SIZE=3D2>Basically, the remove method already shifts all the =
> subsequent data &quot;left&quot;, so, for example,</FONT>
> </P>
> 
> <P><FONT SIZE=3D2>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:</FONT></P>
> 
> <P><FONT SIZE=3D2>abcdABCefg</FONT>
> <BR><FONT SIZE=3D2>but element C failed, then the &quot;count&quot; =
> will be 2, and the lsit will actually be:</FONT>
> <BR><FONT SIZE=3D2>abcdEBefg</FONT>
> <BR><FONT SIZE=3D2>The first iteration of the remove will make the =
> list:</FONT>
> <BR><FONT SIZE=3D2>abcdBefg</FONT>
> <BR><FONT SIZE=3D2>and the second will make it:</FONT>
> <BR><FONT SIZE=3D2>abcdBgf.</FONT>
> </P>
> 
> <P><FONT SIZE=3D2>The remove lines should be:</FONT>
> <BR><FONT =
> SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
> sp; for (int i =3D 0; i &lt; count; i++) {</FONT>
> <BR><FONT =
> SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
> sp;&nbsp;&nbsp;&nbsp;&nbsp; remove(index);</FONT>
> <BR><FONT =
> SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
> sp; }</FONT>
> </P>
> <BR>
> 
> <P><FONT SIZE=3D2>Rolf</FONT>
> </P>
> 
> </BODY>
> </HTML>
> ------_=_NextPart_001_01C304E0.3396236E--
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost
> .com
> 



More information about the jdom-interest mailing list