Bug 26903 - VerifyError when casting null to an array type
Summary: VerifyError when casting null to an array type
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: 2.1 M4   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-11-21 16:38 EST by Olivier Thomann CLA
Modified: 2002-12-17 11:27 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Olivier Thomann CLA 2002-11-21 16:38:24 EST
Using 1119, we do compile the following test case, but we end up with a VerifyError.
Exception in thread "main" java.lang.VerifyError: (class: A, method: main
signature: ([Ljava/lang/String;)V) Incompatible types for storing into array of
arrays or objects

[class A {
    public static void main(String[] args) {
		try {
	    	int[][] tab = (int[][]) null;
	    	tab[0] = new int[1];
		} catch (NullPointerException e) {
		    System.out.print("OK");
		}
    }
}]

The fix for this one seems to be in the CastExpression, in the method:
areTypeCastCompatible....
		if (expressionType == NullBinding) {
/**/			if (castType.isArrayType()) {
				needRuntimeCheckcast = true;
			}/**/
			return; //null is compatible with every thing
		}

Adding the code between the two /**/ fixes the problem. It forces a checkcast to
be generated even if the expression is null.
Jikes 1.17 has the bug too and javac 1.4.1 compiles the code with a checkcast
and therefore doesn't have a VerifyError.
Comment 1 Olivier Thomann CLA 2002-11-21 16:42:33 EST
Almost the same pattern:
class A {
    public static void main(String[] args) {
		try {
                    int[][] tab;
                    tab = null;
                    tab[0] = new int[1];
		} catch (NullPointerException e) {
		    System.out.print("OK");
		}
    }
}

With this code, all compilers (jikes, javac and eclipse) compile fine, but get a
VerifyError at runtime.
Comment 2 Olivier Thomann CLA 2002-11-21 16:53:55 EST
This second bug seems to be in SingleNameReference when generating the code for
a local variable.
We might want to add before the store call:
if (assignment.expression.resolvedType == NullBinding &&
assignment.lhs.resolvedType.isArrayType()) {
      codeStream.checkcast(assignment.lhs.resolvedType);
}
// normal local assignment (since cannot store in outer local which are final
locations)
codeStream.store(localBinding, valueRequired);
Comment 3 Philipe Mulet CLA 2002-11-22 17:22:32 EST
All kind of references would be affected by the same bug.
Need to investigate.
Comment 4 Philipe Mulet CLA 2002-11-25 05:43:35 EST
The problem is that these extra cast are only relevant when using local 
variables, not fields.
Comment 5 Philipe Mulet CLA 2002-11-25 05:55:55 EST
Also, the first scenario is a read herring. The real issue is the second one. 
Solving the second will make the first one work fine as well.
Comment 6 Philipe Mulet CLA 2002-11-25 09:13:25 EST
Fixed
Comment 7 David Audel CLA 2002-12-17 11:27:18 EST
Verified.