0ef202
From 3a4e355796891149adfd9228633f179015293dbd Mon Sep 17 00:00:00 2001
0ef202
From: Richard Atkins <rjatkins359@gmail.com>
0ef202
Date: Wed, 21 Sep 2022 23:18:58 +1000
0ef202
Subject: [PATCH] CVE-2022-42920
0ef202
0ef202
---
0ef202
 .../org/apache/bcel/classfile/ConstantPool.java   | 15 +++++++++++----
0ef202
 .../org/apache/bcel/generic/ConstantPoolGen.java  | 11 ++++++++++-
0ef202
 2 files changed, 21 insertions(+), 5 deletions(-)
0ef202
0ef202
diff --git a/src/main/java/org/apache/bcel/classfile/ConstantPool.java b/src/main/java/org/apache/bcel/classfile/ConstantPool.java
0ef202
index f2c946a1..77ab0da4 100644
0ef202
--- a/src/main/java/org/apache/bcel/classfile/ConstantPool.java
0ef202
+++ b/src/main/java/org/apache/bcel/classfile/ConstantPool.java
0ef202
@@ -218,10 +218,17 @@ public class ConstantPool implements Cloneable, Node {
0ef202
      * @throws IOException
0ef202
      */
0ef202
     public void dump( final DataOutputStream file ) throws IOException {
0ef202
-        file.writeShort(constant_pool.length);
0ef202
-        for (int i = 1; i < constant_pool.length; i++) {
0ef202
-            if (constant_pool[i] != null) {
0ef202
-                constant_pool[i].dump(file);
0ef202
+        /*
0ef202
+         * Constants over the size of the constant pool shall not be written out.
0ef202
+         * This is a redundant measure as the ConstantPoolGen should have already
0ef202
+         * reported an error back in the situation.
0ef202
+        */
0ef202
+        final int size = Math.min(constant_pool.length, Const.MAX_CP_ENTRIES);
0ef202
+
0ef202
+        file.writeShort(size);
0ef202
+        for (int i = 1; i < size; i++) {
0ef202
+            if (constant_pool[i] != null) {
0ef202
+                constant_pool[i].dump(file);
0ef202
             }
0ef202
         }
0ef202
     }
0ef202
diff --git a/src/main/java/org/apache/bcel/generic/ConstantPoolGen.java b/src/main/java/org/apache/bcel/generic/ConstantPoolGen.java
0ef202
index fd0af47e..d3189ba4 100644
0ef202
--- a/src/main/java/org/apache/bcel/generic/ConstantPoolGen.java
0ef202
+++ b/src/main/java/org/apache/bcel/generic/ConstantPoolGen.java
0ef202
@@ -95,7 +95,7 @@ public class ConstantPoolGen {
0ef202
     public ConstantPoolGen(final Constant[] cs) {
0ef202
         final StringBuilder sb = new StringBuilder(DEFAULT_BUFFER_SIZE);
0ef202
 
0ef202
-        size = Math.max(DEFAULT_BUFFER_SIZE, cs.length + 64);
0ef202
+        size = Math.min(Math.max(DEFAULT_BUFFER_SIZE, cs.length + 64), Const.MAX_CP_ENTRIES + 1);
0ef202
         constants = new Constant[size];
0ef202
 
0ef202
         System.arraycopy(cs, 0, constants, 0, cs.length);
0ef202
@@ -224,9 +224,18 @@ public class ConstantPoolGen {
0ef202
     /** Resize internal array of constants.
0ef202
      */
0ef202
     protected void adjustSize() {
0ef202
+        // 3 extra spaces are needed as some entries may take 3 slots
0ef202
+        if (index + 3 >= Const.MAX_CP_ENTRIES + 1) {
0ef202
+            throw new IllegalStateException("The number of constants " + (index + 3)
0ef202
+                    + " is over the size of the constant pool: "
0ef202
+                    + Const.MAX_CP_ENTRIES);
0ef202
+        }
0ef202
+
0ef202
         if (index + 3 >= size) {
0ef202
             final Constant[] cs = constants;
0ef202
             size *= 2;
0ef202
+            // the constant array shall not exceed the size of the constant pool
0ef202
+            size = Math.min(size, Const.MAX_CP_ENTRIES + 1);
0ef202
             constants = new Constant[size];
0ef202
             System.arraycopy(cs, 0, constants, 0, index);
0ef202
         }
0ef202
-- 
0ef202
2.38.1
0ef202