7040104: javac NPE on Object a[]; Object o = (a=null)[0];

Fri, 29 Apr 2011 16:05:02 +0100

author
mcimadamore
date
Fri, 29 Apr 2011 16:05:02 +0100
changeset 989
4c03383f6529
parent 988
7ae6c0fd479b
child 990
9a847a77205d

7040104: javac NPE on Object a[]; Object o = (a=null)[0];
Summary: When a null literal is found on top of stack, if expected type is 1-dimension array no checkcast is emitted
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/jvm/Code.java file | annotate | diff | comparison | revisions
test/tools/javac/T7040104.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/jvm/Code.java	Thu Apr 28 15:05:36 2011 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/Code.java	Fri Apr 29 16:05:02 2011 +0100
     1.3 @@ -479,7 +479,12 @@
     1.4              state.pop(1);// index
     1.5              Type a = state.stack[state.stacksize-1];
     1.6              state.pop(1);
     1.7 -            state.push(types.erasure(types.elemtype(a))); }
     1.8 +            //sometimes 'null type' is treated as a one-dimensional array type
     1.9 +            //see Gen.visitLiteral - we should handle this case accordingly
    1.10 +            Type stackType = a.tag == BOT ?
    1.11 +                syms.objectType :
    1.12 +                types.erasure(types.elemtype(a));
    1.13 +            state.push(stackType); }
    1.14              break;
    1.15          case goto_:
    1.16              markDead();
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/T7040104.java	Fri Apr 29 16:05:02 2011 +0100
     2.3 @@ -0,0 +1,72 @@
     2.4 +/*
     2.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 7040104
    2.30 + * @summary javac NPE on Object a[]; Object o = (a=null)[0];
    2.31 + */
    2.32 +
    2.33 +public class T7040104 {
    2.34 +    public static void main(String[] args) {
    2.35 +        T7040104 t = new T7040104();
    2.36 +        t.test1();
    2.37 +        t.test2();
    2.38 +        t.test3();
    2.39 +        if (t.npeCount != 3) {
    2.40 +            throw new AssertionError();
    2.41 +        }
    2.42 +    }
    2.43 +
    2.44 +    int npeCount = 0;
    2.45 +
    2.46 +    void test1() {
    2.47 +        Object a[];
    2.48 +        try {
    2.49 +            Object o = (a = null)[0];
    2.50 +        }
    2.51 +        catch (NullPointerException npe) {
    2.52 +            npeCount++;
    2.53 +        }
    2.54 +    }
    2.55 +
    2.56 +    void test2() {
    2.57 +        Object a[][];
    2.58 +        try {
    2.59 +            Object o = (a = null)[0][0];
    2.60 +        }
    2.61 +        catch (NullPointerException npe) {
    2.62 +            npeCount++;
    2.63 +        }
    2.64 +    }
    2.65 +
    2.66 +    void test3() {
    2.67 +        Object a[][][];
    2.68 +        try {
    2.69 +            Object o = (a = null)[0][0][0];
    2.70 +        }
    2.71 +        catch (NullPointerException npe) {
    2.72 +            npeCount++;
    2.73 +        }
    2.74 +    }
    2.75 +}

mercurial