3232import java .lang .reflect .Field ;
3333import java .lang .reflect .InvocationTargetException ;
3434import java .lang .reflect .Method ;
35+ import java .nio .BufferUnderflowException ;
3536import java .nio .ByteBuffer ;
3637import java .util .ArrayList ;
3738import java .util .Arrays ;
@@ -149,7 +150,7 @@ public boolean hasAnnotation(AnnotatedElement element, Class<? extends Annotatio
149150 @ SuppressWarnings ("unchecked" )
150151 @ Override
151152 public Class <? extends Annotation >[] getAnnotationTypes (AnnotatedElement element ) {
152- return Arrays .stream (getAnnotationData (element , false )).map (AnnotationValue ::getType ).toArray (Class []::new );
153+ return Arrays .stream (getAnnotationData (element , false )).map (AnnotationValue ::getType ).filter ( t -> t != null ). toArray (Class []::new );
153154 }
154155
155156 public AnnotationValue [] getDeclaredAnnotationData (AnnotatedElement element ) {
@@ -220,15 +221,19 @@ private AnnotationValue[] getDeclaredAnnotationDataFromRoot(AnnotatedElement roo
220221 return NO_ANNOTATIONS ;
221222 }
222223 ByteBuffer buf = ByteBuffer .wrap (rawAnnotations );
223- List <AnnotationValue > annotations = new ArrayList <>();
224- int numAnnotations = buf .getShort () & 0xFFFF ;
225- for (int i = 0 ; i < numAnnotations ; i ++) {
226- AnnotationValue annotation = AnnotationValue .extract (buf , getConstantPool (element ), getContainer (element ), false , false );
227- if (annotation != null ) {
228- annotations .add (annotation );
224+ try {
225+ List <AnnotationValue > annotations = new ArrayList <>();
226+ int numAnnotations = buf .getShort () & 0xFFFF ;
227+ for (int i = 0 ; i < numAnnotations ; i ++) {
228+ AnnotationValue annotation = AnnotationValue .extract (buf , getConstantPool (element ), getContainer (element ), false , false );
229+ if (annotation != null ) {
230+ annotations .add (annotation );
231+ }
229232 }
233+ return annotations .toArray (NO_ANNOTATIONS );
234+ } catch (IllegalArgumentException | BufferUnderflowException ex ) {
235+ return new AnnotationValue []{AnnotationValue .forAnnotationFormatException ()};
230236 }
231- return annotations .toArray (NO_ANNOTATIONS );
232237 });
233238 }
234239
@@ -244,20 +249,24 @@ private AnnotationValue[][] getParameterAnnotationDataFromRoot(Executable rootEl
244249 return NO_PARAMETER_ANNOTATIONS ;
245250 }
246251 ByteBuffer buf = ByteBuffer .wrap (rawParameterAnnotations );
247- int numParameters = buf .get () & 0xFF ;
248- AnnotationValue [][] parameterAnnotations = new AnnotationValue [numParameters ][];
249- for (int i = 0 ; i < numParameters ; i ++) {
250- List <AnnotationValue > parameterAnnotationList = new ArrayList <>();
251- int numAnnotations = buf .getShort () & 0xFFFF ;
252- for (int j = 0 ; j < numAnnotations ; j ++) {
253- AnnotationValue parameterAnnotation = AnnotationValue .extract (buf , getConstantPool (element ), getContainer (element ), false , false );
254- if (parameterAnnotation != null ) {
255- parameterAnnotationList .add (parameterAnnotation );
252+ try {
253+ int numParameters = buf .get () & 0xFF ;
254+ AnnotationValue [][] parameterAnnotations = new AnnotationValue [numParameters ][];
255+ for (int i = 0 ; i < numParameters ; i ++) {
256+ List <AnnotationValue > parameterAnnotationList = new ArrayList <>();
257+ int numAnnotations = buf .getShort () & 0xFFFF ;
258+ for (int j = 0 ; j < numAnnotations ; j ++) {
259+ AnnotationValue parameterAnnotation = AnnotationValue .extract (buf , getConstantPool (element ), getContainer (element ), false , false );
260+ if (parameterAnnotation != null ) {
261+ parameterAnnotationList .add (parameterAnnotation );
262+ }
256263 }
264+ parameterAnnotations [i ] = parameterAnnotationList .toArray (NO_ANNOTATIONS );
257265 }
258- parameterAnnotations [i ] = parameterAnnotationList .toArray (NO_ANNOTATIONS );
266+ return parameterAnnotations ;
267+ } catch (IllegalArgumentException | BufferUnderflowException ex ) {
268+ return new AnnotationValue [][]{new AnnotationValue []{AnnotationValue .forAnnotationFormatException ()}};
259269 }
260- return parameterAnnotations ;
261270 });
262271 }
263272
@@ -273,12 +282,21 @@ private TypeAnnotationValue[] getTypeAnnotationDataFromRoot(AnnotatedElement roo
273282 return NO_TYPE_ANNOTATIONS ;
274283 }
275284 ByteBuffer buf = ByteBuffer .wrap (rawTypeAnnotations );
276- int annotationCount = buf .getShort () & 0xFFFF ;
277- TypeAnnotationValue [] typeAnnotationValues = new TypeAnnotationValue [annotationCount ];
278- for (int i = 0 ; i < annotationCount ; i ++) {
279- typeAnnotationValues [i ] = TypeAnnotationValue .extract (buf , getConstantPool (element ), getContainer (element ));
285+ try {
286+ int annotationCount = buf .getShort () & 0xFFFF ;
287+ TypeAnnotationValue [] typeAnnotationValues = new TypeAnnotationValue [annotationCount ];
288+ for (int i = 0 ; i < annotationCount ; i ++) {
289+ typeAnnotationValues [i ] = TypeAnnotationValue .extract (buf , getConstantPool (element ), getContainer (element ));
290+ }
291+ return typeAnnotationValues ;
292+ } catch (IllegalArgumentException | BufferUnderflowException ex ) {
293+ /*
294+ * The byte[] arrrays in the TypeAnnotationValue are structurally correct, but have
295+ * an illegal first targetInfo byte that will throw an AnnotationFormatException
296+ * during parsing.
297+ */
298+ return new TypeAnnotationValue []{new TypeAnnotationValue (new byte []{0x77 }, new byte []{0 }, AnnotationValue .forAnnotationFormatException ())};
280299 }
281- return typeAnnotationValues ;
282300 });
283301 }
284302
@@ -294,7 +312,11 @@ private AnnotationMemberValue getAnnotationDefaultDataFromRoot(Method accessorMe
294312 return null ;
295313 }
296314 ByteBuffer buf = ByteBuffer .wrap (rawAnnotationDefault );
297- return AnnotationMemberValue .extract (buf , getConstantPool (method ), getContainer (method ), false );
315+ try {
316+ return AnnotationMemberValue .extract (buf , getConstantPool (method ), getContainer (method ), false );
317+ } catch (IllegalArgumentException | BufferUnderflowException ex ) {
318+ return AnnotationValue .forAnnotationFormatException ();
319+ }
298320 });
299321 }
300322
0 commit comments