8182747: javac crashes on bad annotation value
authorvromero
Thu, 29 Jun 2017 07:19:44 -0700
changeset 45753 567c159ce16f
parent 45752 32eb45c44fc8
child 45754 740593904730
8182747: javac crashes on bad annotation value Reviewed-by: mcimadamore
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/test/tools/javac/T8182747/BadAnnotationRegressionTest.java
langtools/test/tools/javac/T8182747/BadAnnotationRegressionTest.out
langtools/test/tools/javac/diags/examples/AnnoMustBeClasLiteral.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java	Wed Jun 28 14:46:18 2017 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java	Thu Jun 29 07:19:44 2017 -0700
@@ -630,6 +630,13 @@
             }
         }
 
+        // Class literals look like field accesses of a field named class
+        // at the tree level
+        if (TreeInfo.name(tree) != names._class) {
+            log.error(tree.pos(), Errors.AnnotationValueMustBeClassLiteral);
+            return new Attribute.Error(syms.errType);
+        }
+
         return new Attribute.Class(types,
                 (((JCFieldAccess) tree).selected).type);
     }
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Jun 28 14:46:18 2017 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Jun 29 07:19:44 2017 -0700
@@ -134,6 +134,9 @@
 compiler.err.annotation.value.must.be.annotation=\
     annotation value must be an annotation
 
+compiler.err.annotation.value.must.be.class.literal=\
+    annotation value must be a class literal
+
 compiler.err.annotation.value.must.be.name.value=\
     annotation values must be of the form ''name=value''
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/T8182747/BadAnnotationRegressionTest.java	Thu Jun 29 07:19:44 2017 -0700
@@ -0,0 +1,45 @@
+/* @test /nodynamiccopyright/
+ * @bug 8182747
+ * @summary javac crashes on bad annotation value
+ * @compile/fail/ref=BadAnnotationRegressionTest.out -XDrawDiagnostics BadAnnotationRegressionTest.java
+ */
+
+class BadAnnotationRegressionTest {
+    @interface ClassAnno {
+        Class<?> value();
+    }
+
+    @interface ArrayAnno {
+        int[] value();
+    }
+
+    @interface PrimitiveAnno {
+        int value();
+    }
+
+    @interface StringAnno {
+        String value();
+    }
+
+    enum E {
+        A,
+        B,
+    }
+
+    @interface EnumAnno {
+        E value();
+    }
+
+    static final Class<?> c = Object.class;
+    static final int i = 0;
+    static final int[] arr = new int[] { 1, 2, 3 };
+    static final E a = E.A;
+    static final String s = "";
+
+    @ClassAnno(c)     // error
+    @PrimitiveAnno(i) // ok
+    @ArrayAnno(arr)   // error
+    @EnumAnno(a)      // error
+    @StringAnno(s)    //ok
+    void testAnno() {}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/T8182747/BadAnnotationRegressionTest.out	Thu Jun 29 07:19:44 2017 -0700
@@ -0,0 +1,4 @@
+BadAnnotationRegressionTest.java:39:16: compiler.err.annotation.value.must.be.class.literal
+BadAnnotationRegressionTest.java:41:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int[], int)
+BadAnnotationRegressionTest.java:42:15: compiler.err.enum.annotation.must.be.enum.constant
+3 errors
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/AnnoMustBeClasLiteral.java	Thu Jun 29 07:19:44 2017 -0700
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2017, 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.
+ */
+
+// key: compiler.err.annotation.value.must.be.class.literal
+
+class EnumAnnoValueMustBeEnumConstant {
+    @interface ClassAnno {
+        Class<?> value();
+    }
+
+    static final Class<?> c = Object.class;
+
+    @ClassAnno(c)
+    void testAnno() {}
+}