From 41ef2b249073450172e11163a4d05762364b1297 Mon Sep 17 00:00:00 2001
From: Joe Wang <joehw@openjdk.org>
Date: Fri, 13 May 2022 02:02:26 +0000
Subject: [PATCH] 8285407: Improve Xalan supports

Reviewed-by: naoto, lancea, ahgross, rhalade
---
 .../bcel/internal/classfile/ConstantPool.java   | 17 +++++++++++++----
 .../bcel/internal/generic/ConstantPoolGen.java  | 17 +++++++++++++----
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/ConstantPool.java b/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/ConstantPool.java
index 303b2b8ad8d8..ed4ce0948ed9 100644
--- a/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/ConstantPool.java
+++ b/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/ConstantPool.java
@@ -1,6 +1,5 @@
 /*
- * reserved comment block
- * DO NOT REMOVE OR ALTER!
+ * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
  */
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
@@ -26,6 +25,7 @@
 import java.io.IOException;
 
 import com.sun.org.apache.bcel.internal.Const;
+import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
 
 /**
  * This class represents the constant pool, i.e., a table of constants, of
@@ -37,6 +37,7 @@
 
  * @see     Constant
  * @see     com.sun.org.apache.bcel.internal.generic.ConstantPoolGen
+ * @LastModified: May 2022
  */
 public class ConstantPool implements Cloneable, Node {
 
@@ -222,8 +223,16 @@ public String constantToString( final int index, final byte tag ) throws ClassFo
      * @throws IOException
      */
     public void dump( final DataOutputStream file ) throws IOException {
-        file.writeShort(constantPool.length);
-        for (int i = 1; i < constantPool.length; i++) {
+        /*
+         * Constants over the size of the constant pool shall not be written out.
+         * This is a redundant measure as the ConstantPoolGen should have already
+         * reported an error back in the situation.
+        */
+        int size = constantPool.length < ConstantPoolGen.CONSTANT_POOL_SIZE - 1 ?
+                constantPool.length : ConstantPoolGen.CONSTANT_POOL_SIZE - 1;
+
+        file.writeShort(size);
+        for (int i = 1; i < size; i++) {
             if (constantPool[i] != null) {
                 constantPool[i].dump(file);
             }
diff --git a/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/generic/ConstantPoolGen.java b/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/generic/ConstantPoolGen.java
index a1dadbfc4b61..806b40da1ed6 100644
--- a/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/generic/ConstantPoolGen.java
+++ b/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/generic/ConstantPoolGen.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
  */
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
@@ -50,10 +50,10 @@
  * JVM and that Double and Long constants need two slots.
  *
  * @see Constant
- * @LastModified: May 2021
+ * @LastModified: May 2022
  */
 public class ConstantPoolGen {
-
+    public static final int CONSTANT_POOL_SIZE = 65536;
     private static final int DEFAULT_BUFFER_SIZE = 256;
     private int size;
     private Constant[] constants;
@@ -83,7 +83,7 @@ private static class Index {
     public ConstantPoolGen(final Constant[] cs) {
         final StringBuilder sb = new StringBuilder(DEFAULT_BUFFER_SIZE);
 
-        size = Math.max(DEFAULT_BUFFER_SIZE, cs.length + 64);
+        size = Math.min(Math.max(DEFAULT_BUFFER_SIZE, cs.length + 64), CONSTANT_POOL_SIZE);
         constants = new Constant[size];
 
         System.arraycopy(cs, 0, constants, 0, cs.length);
@@ -212,9 +212,18 @@ public ConstantPoolGen() {
     /** Resize internal array of constants.
      */
     protected void adjustSize() {
+        // 3 extra spaces are needed as some entries may take 3 slots
+        if (index + 3 >= CONSTANT_POOL_SIZE) {
+            throw new RuntimeException("The number of constants " + (index + 3)
+                    + " is over the size of the constant pool: "
+                    + (CONSTANT_POOL_SIZE - 1));
+        }
+
         if (index + 3 >= size) {
             final Constant[] cs = constants;
             size *= 2;
+            // the constant array shall not exceed the size of the constant pool
+            size = Math.min(size, CONSTANT_POOL_SIZE);
             constants = new Constant[size];
             System.arraycopy(cs, 0, constants, 0, index);
         }
