JDK-4026541 : java.awt.Container.remove(int i) throws different exceptions if i is invalid
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_2.5
  • CPU: sparc
  • Submitted: 1997-01-17
  • Updated: 2001-11-28
  • Resolved: 2001-11-28
Related Reports
Relates :  
Description

Name: saC57035			Date: 01/17/97



The java.awt.Container.remove(int index) throws different exceptions 
if index is out of bounds. The NullPointerException in this case is 
quite unexpected.

Here is the example demonstrating the bug:

---- Test.java ----------------------
import java.awt.*;

public class Test {

public static void main( String argv[] ) {
 Panel p = new Panel();
 for(int i=-2;i<6;i++)
   try {
     p.remove(i);
   } catch(Exception e) {
     System.out.println("Removing "+i+" : "+e); 
   }
}

}
-- The output ----------------
Removing -2 : java.lang.ArrayIndexOutOfBoundsException: -2
Removing -1 : java.lang.ArrayIndexOutOfBoundsException: -1
Removing 0 : java.lang.NullPointerException
Removing 1 : java.lang.NullPointerException
Removing 2 : java.lang.NullPointerException
Removing 3 : java.lang.NullPointerException
Removing 4 : java.lang.ArrayIndexOutOfBoundsException: 4
Removing 5 : java.lang.ArrayIndexOutOfBoundsException: 5
------------------------------

======================================================================



Modifying the above test as follows:

java.awt.*;

public class Test {

    public static void main( String argv[] ) {
         Panel p = new Panel();
         Canvas c;

        for (int i=0;i<100;i++) {
            c = new Canvas();
            p.add(c);
        }

       int[] bad={-20, Integer.MIN_VALUE, Integer.MAX_VALUE, 113};
       for(int i = 0; i < bad.length; i++){
           try {
             p.remove(bad[i]);
           } catch(Exception e) {
             System.out.println("Removing "+ bad[i]+" : "+e);
           }
       }
    }
}

results in the following output <from 

Removing -20 : java.lang.ArrayIndexOutOfBoundsException
Removing -2147483648 : java.lang.ArrayIndexOutOfBoundsException
Removing 2147483647 : java.lang.ArrayIndexOutOfBoundsException
Removing 113 : java.lang.NullPointerException

Should the removal of the element at position 113 of the container array not result in an ArrayIndexOutOfBoundsException as this position is out of bounds of the array?

###@###.### 2001-10-17

Comments
WORK AROUND 5/5/2000 kevin.ryan@eng -- check result of Container.getComponentCount() first.
11-06-2004

EVALUATION Name: rrT76497 Date: 06/04/98 G Mohan Raj, SIPTech 05/05/98 testcase - RemoveTest Bug reproduced, component array in the Container by default is set to the size of 4 If we try to remove within the valid range(0 - 3), remove() method reference a non existant component, therefore we get a NullPointer exception. Otherwise we get ArrayIndexOutOfBoundsException. An IllegalArgumentException may be thrown for the negetive values. testcase---------- /** * @bug 4026541 * @summary The exception throwns when removing non existing components from a container * are added to a vector and verified whether it is consistent throughout. * @run application/auto * @author G Mohan Raj */ import java.awt.Panel; import java.util.Vector; public class RemoveTest { public static void main( String argv[] ) { Panel p = new Panel(); Vector v = new Vector(); for(int i=-2;i<6;i++) { try { p.remove(i); } catch(Exception e) { v.addElement(e); } } for(int x=0;x<v.size();x++) { for(int y=0;x<v.size();y++) { if(!v.elementAt(x).equals(v.elementAt(y))) { throw new RuntimeException("Inconsistent exceptions are thrown"); } } } } } ====================================================================== 5/5/2000 kevin.ryan@eng -- check Container.getComponentCount() first (if you don't already know the # of components you're holding), and don't try to remove any that don't exist. Re. the "inconsistency" of exceptions: though it's inconsistent that component count is 0, while four default "slots" for components have been allocated, the exceptions seem to be appropriate for the two cases: a) slot allocated (valid array index), but component doesn't exist: array index of slot is "valid", but since slot contents point(s) to nothing, NPE given (seems appropriate) b) slot not allocated (<0, or >3): ArrayIndexOutOfBoundsExceptions seems appropriate It's not clear this is a bug. ========================================================= In the first example given by the submitter the result is... Removing -2 : java.lang.ArrayIndexOutOfBoundsException Removing -1 : java.lang.ArrayIndexOutOfBoundsException Removing 0 : java.lang.ArrayIndexOutOfBoundsException Removing 1 : java.lang.ArrayIndexOutOfBoundsException Removing 2 : java.lang.ArrayIndexOutOfBoundsException Removing 3 : java.lang.ArrayIndexOutOfBoundsException Removing 4 : java.lang.ArrayIndexOutOfBoundsException Removing 5 : java.lang.ArrayIndexOutOfBoundsException The second example yields.. Removing -20 : java.lang.ArrayIndexOutOfBoundsException Removing -2147483648 : java.lang.ArrayIndexOutOfBoundsException Removing 2147483647 : java.lang.ArrayIndexOutOfBoundsException Removing 113 : java.lang.NullPointerException I don't see either as a bug. Tested with Merlin build 86 ###@###.### 2001-11-16 ------ JCK 1.4 test api/java_awt/Container/index.html#remove, test case [Container2017] still fails due to this bug (method throws unexpected NullPoineterException). You can find the latest test's source code in: /java/re/jck/1.4/promoted/latest/binaries/JCK-runtime-14/tests/api/java_awt/Container/removeTests.java NOTE: Documentation does not declare the behavior of remove(int) method if parameter is invalid. Javadoc bug that describe this fault will be filed separately. Here is .jtr file: #Test Results (version 2) #Tue Nov 27 20:57:10 GMT+03:00 2001 #checksum:5d882df89b12b9f9 #-----testdescription----- $file=/home/sda/jck-merlin-sda/tests/api/java_awt/Container/index.html $root=/home/sda/jck-merlin-sda/tests executeClass=javasoft.sqe.tests.api.java.awt.Container.removeTests id=remove keywords=positive runtime selectIf=!platform.isHeadless source=removeTests.java StubLayout.java title=Container remove tests #-----environment----- DISPLAY=sword:0.0 ExecJCKTestOtherJVMCmd=javasoft.sqe.javatest.lib.ExecJCKTestOtherJVMCmd classesUnderTest=/home/sda/log/classes command.testExecute=$ExecJCKTestOtherJVMCmd CLASSPATH=$classesUnderTest$:$javatestClassDir DISPLAY=$DISPLAY HOME=${user.home} LD_LIBRARY_PATH=$testSuiteRootDir/../lib $javaUnderTest -Xfuture -Djava.security.policy=$policyFile -Djava.security.auth.policy==$testSuiteRootDir/../lib/jck.auth.policy -Djava.security.auth.login.config==$testSuiteRootDir/../lib/jck.auth.login.config -Djava.rmi.activation.port=${java.rmi.activation.port} $testExecuteClass $testExecuteArgs javaUnderTest=/set/jdk-builds/JDK1.4.0beta2-b87/solaris/bin/java policyFile=/home/sda/teamwork/jck/lib/jck.policy user.home=/home/sda #-----testresult----- description=file:///home/sda/jck-merlin-sda/tests/api/java_awt/Container/index.html#remove end=Tue Nov 27 20:57:10 GMT+03:00 2001 environment=jck-runtime-solaris execStatus=Failed. tests: 3; passed: 2; failed: 1; first test case failure: Container2017 javatestOS=SunOS 5.8 (sparc) javatestVersion=2.1.6 script=com.sun.jck.lib.JCKScript sections=script_messages testExecute start=Tue Nov 27 20:57:07 GMT+03:00 2001 status=Failed. tests: 3; passed: 2; failed: 1; first test case failure: Container2017 test=api/java_awt/Container/index.html#remove timeoutSeconds=600 work=/home/sda/log/test/jck-runtime-solaris/work/api/java_awt/Container #section:script_messages ----------messages:(1/24)---------- Executing test class... #section:testExecute ----------messages:(1/594)---------- command: javasoft.sqe.javatest.lib.ExecJCKTestOtherJVMCmd CLASSPATH=/home/sda/log/classes:/set/java-sqe/javatest/javatest.jar DISPLAY=sword:0.0 HOME=/home/sda LD_LIBRARY_PATH=/home/sda/jck-merlin-sda/tests/../lib /set/jdk-builds/JDK1.4.0beta2-b87/solaris/bin/java -Xfuture -Djava.security.policy=/home/sda/teamwork/jck/lib/jck.policy -Djava.security.auth.policy==/home/sda/jck-merlin-sda/tests/../lib/jck.auth.policy -Djava.security.auth.login.config==/home/sda/jck-merlin-sda/tests/../lib/jck.auth.login.config -Djava.rmi.activation.port= javasoft.sqe.tests.api.java.awt.Container.removeTests ----------ref:(3/46)---------- Removing -20 Removing -2147483648 Removing 50 ----------log:(15/974)---------- Container2016: Passed. OKAY java.lang.NullPointerException at java.awt.Container.remove(Container.java:540) at javasoft.sqe.tests.api.java.awt.Container.removeTests.Container2017(removeTests.java:113) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:42) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:28) at java.lang.reflect.Method.invoke(Method.java:327) at javasoft.sqe.javatest.lib.MultiTest.invokeTestCase(MultiTest.java:407) at javasoft.sqe.javatest.lib.MultiTest.run(MultiTest.java:203) at javasoft.sqe.javatest.lib.MultiTest.run(MultiTest.java:135) at javasoft.sqe.tests.api.java.awt.Container.removeTests.main(removeTests.java:35) Container2017: Failed. Test case throws exception: java.lang.NullPointerException Container0004: Passed. OKAY STATUS:Failed.tests: 3; passed: 2; failed: 1; first test case failure: Container2017 result: Failed. tests: 3; passed: 2; failed: 1; first test case failure: Container2017 test result: Failed. tests: 3; passed: 2; failed: 1; first test case failure: Container2017 So, I reopen this bug. ###@###.### 2001-11-27 ========== This bug was filed against 1.1beta2. If this problem existed in 1.1beta2, it is not possible that it is causing a new compatibility problem in 1.4. If there is a JCK test failing in 1.4, a new bug needs to be filed against that release. I am closing this report. ###@###.### 2001-11-28
28-11-2001