From 81dc6e267babbca337c058dacb27ab9335027cf8 Mon Sep 17 00:00:00 2001 From: JOO200 Date: Sun, 19 Apr 2020 00:41:22 +0200 Subject: [PATCH] Don't allow infinite recursive annotations (#258) --- core/src/main/java/co/aikar/commands/Annotations.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/co/aikar/commands/Annotations.java b/core/src/main/java/co/aikar/commands/Annotations.java index 48ef4e33..92e840a2 100644 --- a/core/src/main/java/co/aikar/commands/Annotations.java +++ b/core/src/main/java/co/aikar/commands/Annotations.java @@ -27,6 +27,8 @@ import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Collection; +import java.util.HashSet; import java.util.IdentityHashMap; import java.util.Map; @@ -50,7 +52,7 @@ class Annotations extends AnnotationLookups { } String getAnnotationValue(AnnotatedElement object, Class annoClass, int options) { - Annotation annotation = getAnnotationRecursive(object, annoClass); + Annotation annotation = getAnnotationRecursive(object, annoClass, new HashSet<>()); String value = null; if (annotation != null) { @@ -103,13 +105,15 @@ class Annotations extends AnnotationLookups { return value; } - private static Annotation getAnnotationRecursive(AnnotatedElement object, Class annoClass) { + private static Annotation getAnnotationRecursive(AnnotatedElement object, Class annoClass, Collection checked) { if (object.isAnnotationPresent(annoClass)) { return object.getAnnotation(annoClass); } else { for (Annotation otherAnnotation : object.getDeclaredAnnotations()) { if (!otherAnnotation.annotationType().getPackage().getName().startsWith("java.")) { - final Annotation foundAnnotation = getAnnotationRecursive(otherAnnotation.annotationType(), annoClass); + if (checked.contains(otherAnnotation)) return null; + checked.add(otherAnnotation); + final Annotation foundAnnotation = getAnnotationRecursive(otherAnnotation.annotationType(), annoClass, checked); if (foundAnnotation != null) { return foundAnnotation; }