8071847: java.lang.NullPointerException at com.sun.tools.javac.code.Types.elemtype(Types.java:1870)
authorsadayapalam
Fri, 13 Mar 2015 13:15:24 +0530
changeset 29551 cad66be66e2e
parent 29433 c97e2d1bad97
child 29552 690d0becaf32
8071847: java.lang.NullPointerException at com.sun.tools.javac.code.Types.elemtype(Types.java:1870) Summary: Class file reader should recover from malformed class files that contain methods incorrectly flagged as variable arity methods. Reviewed-by: jlahoda
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/test/tools/javac/T8071847/T8071847.java
langtools/test/tools/javac/diags/examples.not-yet.txt
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Wed Jul 05 20:24:25 2017 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Fri Mar 13 13:15:24 2015 +0530
@@ -56,6 +56,7 @@
 
 import static com.sun.tools.javac.code.Flags.*;
 import static com.sun.tools.javac.code.Kinds.Kind.*;
+import static com.sun.tools.javac.code.TypeTag.ARRAY;
 import static com.sun.tools.javac.code.TypeTag.CLASS;
 import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
 import static com.sun.tools.javac.jvm.ClassFile.*;
@@ -2006,6 +2007,15 @@
         }
         if (saveParameterNames)
             setParameterNames(m, type);
+
+        if ((flags & VARARGS) != 0) {
+            final Type last = type.getParameterTypes().last();
+            if (last == null || !last.hasTag(ARRAY)) {
+                m.flags_field &= ~VARARGS;
+                throw badClassFile("malformed.vararg.method", m);
+            }
+        }
+
         return m;
     }
 
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Jul 05 20:24:25 2017 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Mar 13 13:15:24 2015 +0530
@@ -1847,6 +1847,9 @@
 compiler.misc.undecl.type.var=\
     undeclared type variable: {0}
 
+compiler.misc.malformed.vararg.method=\
+    class file contains malformed variable arity method: {0}
+
 compiler.misc.wrong.version=\
     class file has wrong version {0}.{1}, should be {2}.{3}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/T8071847/T8071847.java	Fri Mar 13 13:15:24 2015 +0530
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * bug 8071847
+ * @summary Verify proper termination when instance initialization method uses invalid flags
+ * @compile  T8071847.java
+ * @run main T8071847
+*/
+
+
+import java.io.*;
+import java.util.*;
+
+public class T8071847 {
+    String testclass="invalidFlags.class";
+    String testclassHexString =
+    "CAFEBABE00000031000D0A0003000A07000B07000C0100063C696E69743E0100" +
+    "03282956010004436F646501000F4C696E654E756D6265725461626C6501000A" +
+    "536F7572636546696C65010009546573742E6A6176610C0004000501000C696E" +
+    "76616C6964466C6167730100106A6176612F6C616E672F4F626A656374002000" +
+    "02000300000000000100A000040005000100060000001D00010001000000052A" +
+    "B70001B10000000100070000000600010000000100010008000000020009";
+
+    String testJavaFile = "testInvalidFlags.java";
+    String testJavaSource ="public class testInvalidFlags extends invalidFlags {" +
+        "invalidFlags c = null;" +
+        "public testInvalidFlags() {  c = new invalidFlags(); }" +
+        "public static void main(String... args) { " +
+        "new testInvalidFlags();}}";
+
+    public static void main(String[] args) throws Exception {
+        new  T8071847().run();
+    }
+
+    public void run() throws IOException {
+        writeHexFile(testclass,testclassHexString);
+        writeTestFile(testJavaFile, testJavaSource);
+        javac(testJavaFile);
+    }
+
+    File writeTestFile(String fname, String source) throws IOException {
+        File f = new File(fname);
+        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
+        out.println(source);
+        out.close();
+        return f;
+    }
+
+    byte[] hexToByte(String str) {
+        char[] CA = str.toCharArray();
+        byte[] byteArry = new byte[str.length()/2];
+        int bi = 0;
+        for (int i = 0; i<CA.length ; i+=2) {
+            char c1 = CA[i], c2=CA[i+1];
+            byteArry[bi++] = (byte)((Character.digit((int)c1,16)<<4) +
+                             Character.digit((int)c2,16));
+        }
+        return byteArry;
+    }
+
+    File writeHexFile(String classFileName, String hexString) throws IOException {
+        File f = new File(classFileName);
+        FileOutputStream output  = new FileOutputStream(f);
+        output.write(hexToByte(hexString));
+        output.close();
+        return f;
+    }
+
+    String javac(String className) {
+        StringWriter sw = new StringWriter();
+        PrintWriter out = new PrintWriter(sw);
+        int rc = 0;
+        List<String> javacArgs = new ArrayList<>();
+        javacArgs.addAll(Arrays.asList("-XDrawDiagnostics", "-cp", ".", "-d", ".", className));
+        rc = com.sun.tools.javac.Main.compile(
+            javacArgs.toArray(new String[javacArgs.size()]),out);
+        out.close();
+        if (rc > 1) {
+            System.out.println(sw.toString());
+            throw new Error("javac " + className + " failed. rc=" + rc);
+        }
+        if (rc != 1 || !sw.toString().contains("compiler.misc.malformed.vararg.method"))
+            throw new RuntimeException("Unexpected output" + sw.toString());
+        return sw.toString();
+    }
+}
--- a/langtools/test/tools/javac/diags/examples.not-yet.txt	Wed Jul 05 20:24:25 2017 +0200
+++ b/langtools/test/tools/javac/diags/examples.not-yet.txt	Fri Mar 13 13:15:24 2015 +0530
@@ -87,6 +87,7 @@
 compiler.misc.unable.to.access.file                     # ClassFile
 compiler.misc.undecl.type.var                           # ClassReader
 compiler.misc.unicode.str.not.supported                 # ClassReader
+compiler.misc.malformed.vararg.method                   # ClassReader
 compiler.misc.verbose.retro                             # UNUSED
 compiler.misc.verbose.retro.with                        # UNUSED
 compiler.misc.verbose.retro.with.list                   # UNUSED