JDK-6570354 : DefaultPersistenceDelegate throws an ArrayIndexOutOfBoundsException
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.beans
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-06-15
  • Updated: 2011-05-18
  • Resolved: 2011-05-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b20Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :


A DESCRIPTION OF THE PROBLEM :
at last of  method initBean(Class type, Object oldInstance, Object newInstance, Encoder out):
line 316:
String removeListenerMethodName = d.getRemoveListenerMethod().getName();
            for (int i = oldL.length; i < newL.length; i++) {
                invokeStatement(oldInstance, removeListenerMethodName, new Object[]{oldL[i]}, out);
// the upper line should be
invokeStatement(oldInstance, removeListenerMethodName, new Object[]{newL[i]}, out);

            }

It is obvious that if the for loop is executed, you are trying to access "oldL" with an illegal index because i >= oldL.length.
I think this may be a clerical error and "newL" should be used inside the for loop.

The exception will be thrown when we remove some default listeners from an instance. In one word, if oldL.length < newL.length,
an ArrayIndexOutOfBoundsException will be thrown.


REPRODUCIBILITY :
This bug can be reproduced occasionally.


-------------  begin ----------------
/*
 * Test.java
 *
 * Created on June 15, 2007, 2:41 PM
 *
 */

import java.awt.BorderLayout;
import java.beans.ExceptionListener;
import java.beans.XMLEncoder;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicLabelUI;

public class Test {
    
    /** Creates a new instance of Test */
    public Test() {
        JPanel panel = new JPanel(new BorderLayout());
        JLabel label = new JLabel("");
        //the following line is ugly and useless
        //but I have to do something to detonate the bomb
        label.removePropertyChangeListener((BasicLabelUI)label.getUI());
        
        panel.add(label);
        
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        XMLEncoder encoder =  new XMLEncoder(out);
        encoder.setExceptionListener(new ExceptionListener() {
            public void exceptionThrown(Exception e) {
                e.printStackTrace();
                System.err.println("Continuing ...");
            }
        });
        encoder.writeObject(panel);
        encoder.flush();
        encoder.close();
        String xml;
        try {
            xml = new String(out.toByteArray(), "UTF-8");
            System.out.println(xml);
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }

    }
    
    public static void main(String[] args) {
        Test app = new Test();
    }
}
-------------  end ----------------

Comments
SUGGESTED FIX DefaultPersistenceDelegate.java: String removeListenerMethodName = d.getRemoveListenerMethod().getName(); for (int i = oldL.length; i < newL.length; i++) { - invokeStatement(oldInstance, removeListenerMethodName, new Object[]{oldL[i]}, out); + invokeStatement(oldInstance, removeListenerMethodName, new Object[]{newL[i]}, out); }
19-06-2007

EVALUATION This problem was found while some other bugs were investigated. We should fix this bug before the following bugs: 4667053 & 6243086.
19-06-2007