[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools] DEFECT REPORT: compiler generating invalid bytecode

This problem has been reported in the workbench group but I'm going to post it here as well sice this group has the more technical slant to it. The Eclipse compiler fails to produce a valid class file for the following code:

public class LocalBug {
   public static void main(String[] args) {
     int foo;
     //foo = 0;
     for (int i = 0; i < 1; i++) {
       try {
         foo = 1;
       } catch (Exception e) {
         //foo = 0;
         continue;
       }
     }
   }
}

The code fails with a runtime error:

===========================
java.lang.ClassFormatError: LocalBug (Invalid start_pc/length in local var table)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:496)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:117)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$300(URLClassLoader.java:69)
at java.net.URLClassLoader$ClassFinder.run(URLClassLoader.java:544)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:203)
at java.lang.ClassLoader.loadClass(ClassLoader.java:325)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:257)
Exception in thread "main"
============================


The bytecode for the main method is as follows:

============================
Method void main(java.lang.String[])
    0 iconst_0
    1 istore_1
    2 goto 14
    5 iconst_0
    6 istore_2
    7 goto 11
   10 astore_3
   11 iinc 1 1
   14 iload_1
   15 iconst_2
   16 if_icmplt 5
   19 return
Exception table:
    from   to  target type
      5    10    10   <Class java.lang.Exception>
==============================

Note that the exception handler target is the same program counter value as the 'to' portion of the exception handler range. The same code compiled with the Sun JDK 1.3.1 compiler yields this bytecode:

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

Method void main(java.lang.String[])
    0 iconst_0
    1 istore_1
    2 goto 14
    5 iconst_0
    6 istore_2
    7 goto 11
   10 astore_3
   11 iinc 1 1
   14 iload_1
   15 iconst_2
   16 if_icmplt 5
   19 return
Exception table:
    from   to  target type
      5     7    10   <Class java.lang.Exception>
==================================

The bytecode is identical except for the exception handler range which is 5 to 7 instead of 5 to 10.

The problem seems to stem from the fact that the variable 'foo' isn't explicitly initialized in each execution path. If you uncomment either line noted above, the code will work as expected.

Jim S.