#44 org.xml.sax.helper.AttributesImpl bug ?

SAX_2.0
closed-fixed
nobody
5
2003-05-29
2002-06-25
Anonymous
No

Hi,
I think of having found a bug in the AttributesImpl
class (SAX2 interface , XERCES 2.0.1 implementation).

If i execute the following code :
public void startElement(java.lang.String uri,
java.lang.String localName, java.lang.String qName,
org.xml.sax.Attributes attributes) throws
org.xml.sax.SAXException {
//Create new attributes :
org.xml.sax.Attributes newAttributes = new
org.xml.sax.helpers.AttributesImpl();
newAttributes.setAttributes(attributes);
newAttributes.addAttribute("aa", "aa", "aa", "aa",
"aa");
System.out.println("Message not printed.");
}

addAttribute() loops ad infinitum on an element without
attributes (only in this case) like <tag>...</tag>.

I have looked at the source code of AttributesImpl
class and I think i found the bug :

the setAttributes method is like :
public void setAttributes (Attributes atts) {
clear();
length = atts.getLength();
data = new String[length*5];
for (int i = 0; i < length; i++) {
data[i*5] = atts.getURI(i);
data[i*5+1] = atts.getLocalName(i);
data[i*5+2] = atts.getQName(i);
data[i*5+3] = atts.getType(i);
data[i*5+4] = atts.getValue(i);
}
}
A possible correction for this bug (not the only one) is :
public void setAttributes (Attributes atts) {
clear();
length = atts.getLength();
if (length == 0)
data = null;
else
data = new String[length*5];
for (int i = 0; i < length; i++) {
data[i*5] = atts.getURI(i);
data[i*5+1] = atts.getLocalName(i);
data[i*5+2] = atts.getQName(i);
data[i*5+3] = atts.getType(i);
data[i*5+4] = atts.getValue(i);
}
}
Because you do not initialize the object as in the
constructor (but however you crush completely the
attributes
that already exist).

After calling setAttributes() with an empty
'attributes' object (length = 0), when i call
addAttribute(), the AttributesImpl loops in
ensureCapacity() :
while (max < n * 5) {
max *= 2;
}
because max == 0 !

Cordially.

Arnaud W.

e-mail : arnaud.witschger@e-xmlmedia.fr

Discussion

  • Nicholas Cull
    Nicholas Cull
    2002-07-03

    Logged In: YES
    user_id=137221

    Hi Arnaud,

    This was fixed in the SAX 2.0.1 release. The fix was also
    included in the Xerces 2.0.2 release. Using that version of
    Xerces should sort the problem out for you.

    Regards,

    Nicholas.
    run2000@users.sourceforge.net

     
  • David Brownell
    David Brownell
    2003-05-29

    Logged In: YES
    user_id=44117

    As the other updater noted, fixed. The problem
    was Xerces not using the current code.

     
  • David Brownell
    David Brownell
    2003-05-29

    • status: open --> closed-fixed