Incorrect stackmap calculation for the catch block
I'm attaching a small reproducer for the problem I have - it generates bytecode that fails to validate
both with ASM 4.2 and latest 5.0-BETA from Maven Central. The issue seems to be as follows:
- I have a local variable, declared Object.
- I store null into it.
- Then I enter a try block.
- In the try block, I store a String into the variable.
- Still in the try block, I store an Integer into the variable.
- Then I return it. Try block ends here.
- Then I create a catch block, which only does an ATHROW.
The Java code that compiles to bytecode identical to the one emitted by the reproducer is:
public class X {
private static String S;
private static Integer I;
public static Object foo() {
Object x = null;
try {
x = S;
x = I;
return x;
} catch(Throwable t) {
throw t;
}
}
}
The stack map for the catch block presumes the type of the variable is Integer, when in reality, it
should be Object, as the common supertype of String and Integer.
Interestingly, the bug only occurs if I start the method with ACONST_NULL, ASTORE 0 still outside the
try block. If I don't explicitly initialize the variable to null outside the try block, or move this
initialization inside the try block, then the generated stack map is correct.