You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/03/19 13:01:40 UTC

[commons-bcel] branch master updated: Fix null pointers in AnnotationEntry (#213)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-bcel.git


The following commit(s) were added to refs/heads/master by this push:
     new d1ef29c9 Fix null pointers in AnnotationEntry (#213)
d1ef29c9 is described below

commit d1ef29c9546fba6901cf7c180ed850825282aacc
Author: nbauma109 <nb...@users.noreply.github.com>
AuthorDate: Sun Mar 19 14:01:35 2023 +0100

    Fix null pointers in AnnotationEntry (#213)
    
    * added failing unit test
    
    * fixed failing unit test on AnnotationEntry
    
    * added final keywords and fixed trailing whitespaces
    
    * fix after review
    
    * Javadoc
    
    ---------
    
    Co-authored-by: nbauma109 <nb...@github.com>
    Co-authored-by: Gary Gregory <ga...@users.noreply.github.com>
---
 .../org/apache/bcel/classfile/AnnotationEntry.java |  2 +-
 .../java/org/apache/bcel/AnnotationEntryTest.java  | 60 ++++++++++++++++++++++
 2 files changed, 61 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java b/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java
index 849ae873..94633304 100644
--- a/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java
+++ b/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java
@@ -50,7 +50,6 @@ public class AnnotationEntry implements Node {
     public static AnnotationEntry read(final DataInput input, final ConstantPool constantPool, final boolean isRuntimeVisible) throws IOException {
         final AnnotationEntry annotationEntry = new AnnotationEntry(input.readUnsignedShort(), constantPool, isRuntimeVisible);
         final int numElementValuePairs = input.readUnsignedShort();
-        annotationEntry.elementValuePairs = new ArrayList<>();
         for (int i = 0; i < numElementValuePairs; i++) {
             annotationEntry.elementValuePairs
                 .add(new ElementValuePair(input.readUnsignedShort(), ElementValue.readElementValue(input, constantPool), constantPool));
@@ -70,6 +69,7 @@ public class AnnotationEntry implements Node {
         this.typeIndex = typeIndex;
         this.constantPool = constantPool;
         this.isRuntimeVisible = isRuntimeVisible;
+        this.elementValuePairs = new ArrayList<>();
     }
 
     /**
diff --git a/src/test/java/org/apache/bcel/AnnotationEntryTest.java b/src/test/java/org/apache/bcel/AnnotationEntryTest.java
new file mode 100644
index 00000000..7a3b7808
--- /dev/null
+++ b/src/test/java/org/apache/bcel/AnnotationEntryTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.bcel;
+
+import org.apache.bcel.classfile.AnnotationEntry;
+import org.apache.bcel.classfile.ElementValuePair;
+import org.apache.bcel.classfile.SimpleElementValue;
+import org.junit.jupiter.api.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Tests {@link AnnotationEntry}.
+ */
+public class AnnotationEntryTest {
+
+    @Test
+    public void testAddElementNameValuePair() {
+        final AnnotationEntry annotationEntry = new AnnotationEntry(0, null, false);
+        annotationEntry.addElementNameValuePair(new ElementValuePair(0, new SimpleElementValue(0, 0, null), null));
+        assertEquals(1, annotationEntry.getNumElementValuePairs());
+    }
+
+    @Test
+    public void testDump() throws IOException {
+        final ByteArrayOutputStream out = new ByteArrayOutputStream();
+        new AnnotationEntry(0, null, false).dump(new DataOutputStream(out));
+        assertArrayEquals(new byte[4], out.toByteArray());
+    }
+
+    @Test
+    public void testGetElementValuePairs() {
+        assertEquals(0, new AnnotationEntry(0, null, false).getElementValuePairs().length);
+    }
+
+    @Test
+    public void testGetNumElementValuePairs() {
+        assertEquals(0, new AnnotationEntry(0, null, false).getNumElementValuePairs());
+    }
+}