[jdom-interest] How to "crash" JDOM, part 1

trebor.a.rude at lmco.com trebor.a.rude at lmco.com
Thu Aug 31 07:50:52 PDT 2000

	I guess what I should have said was that it couldn't be demonstrated
that there was a problem with getCopy() given the sample code, since it was
only tangentially involved. Looking at it, it seems to be fine, but it does
indicate why the infinite loop that Aleksi saw occured: the parent of the
Element returned by clone() is set to null, which allowed the
"bar.addContent(bar)" statement to work. What are your thoughts on adding
the loop prevention code?  Too much of a performance hit? If you think it's
a good idea, I'd be happy to do the work.

Trebor A. Rude
trebor.a.rude at lmco.com

> -----Original Message-----
> From:	Jason Hunter [SMTP:jhunter at collab.net]
> Sent:	Wednesday, August 30, 2000 4:06 PM
> To:	trebor.a.rude at lmco.com
> Cc:	aleksi.kallio at infomates.net; jdom-interest at jdom.org
> Subject:	Re: [jdom-interest] How to "crash" JDOM, part 1
> trebor.a.rude at lmco.com wrote:
> > 
> > So this should work:
> > 
> > Element foo = new Element(foo);
> > Element bar = foo.getCopy("bar");
> > foo.addContent(bar);
> > 
> > But that isn't what happened in the first case, what happened 
> > amounts to this:
> > 
> > bar.addContent(bar);
> > 
> > So bar adds a pointer to itself in its content list, which causes the
> > infinite loop when trying to traverse the tree in XMLOutputter, and 
> > that's what shouldn't be allowed: adding a pointer to yourself to 
> > your children. It won't fully prevent loops, but it should help.
> Like I said, if the getCopy() isn't doing a full clone then that's a bug
> and you should feel free to roll up your sleeves and figure it out.
> I think you're right there's another issue that the bar.addContent(bar)
> command could cause a corrupted tree if bar doesn't have a parent.  We
> have logic that an element with an existing parent can't be added to
> another element (you must remove it from its original parent first) but
> an element just hovering there could theoretically be added to itself,
> or to its grandchild or great-grandchild or even its child's child's
> child's child.  Eliminating the possibility of a loop would require a
> logic as part of every add that looked at the merge point and walks up
> the tree to the root making sure you never find yourself in the process.
> -jh-

More information about the jdom-interest mailing list