|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | */
|
4 | 4 | /*
|
5 | 5 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
|
50 | 50 | * JVM and that Double and Long constants need two slots.
|
51 | 51 | *
|
52 | 52 | * @see Constant
|
53 |
| - * @LastModified: May 2021 |
| 53 | + * @LastModified: May 2022 |
54 | 54 | */
|
55 | 55 | public class ConstantPoolGen {
|
56 |
| - |
| 56 | + public static final int CONSTANT_POOL_SIZE = 65536; |
57 | 57 | private static final int DEFAULT_BUFFER_SIZE = 256;
|
58 | 58 | private int size;
|
59 | 59 | private Constant[] constants;
|
@@ -83,7 +83,7 @@ private static class Index {
|
83 | 83 | public ConstantPoolGen(final Constant[] cs) {
|
84 | 84 | final StringBuilder sb = new StringBuilder(DEFAULT_BUFFER_SIZE);
|
85 | 85 |
|
86 |
| - size = Math.max(DEFAULT_BUFFER_SIZE, cs.length + 64); |
| 86 | + size = Math.min(Math.max(DEFAULT_BUFFER_SIZE, cs.length + 64), CONSTANT_POOL_SIZE); |
87 | 87 | constants = new Constant[size];
|
88 | 88 |
|
89 | 89 | System.arraycopy(cs, 0, constants, 0, cs.length);
|
@@ -212,9 +212,18 @@ public ConstantPoolGen() {
|
212 | 212 | /** Resize internal array of constants.
|
213 | 213 | */
|
214 | 214 | protected void adjustSize() {
|
| 215 | + // 3 extra spaces are needed as some entries may take 3 slots |
| 216 | + if (index + 3 >= CONSTANT_POOL_SIZE) { |
| 217 | + throw new RuntimeException("The number of constants " + (index + 3) |
| 218 | + + " is over the size of the constant pool: " |
| 219 | + + (CONSTANT_POOL_SIZE - 1)); |
| 220 | + } |
| 221 | + |
215 | 222 | if (index + 3 >= size) {
|
216 | 223 | final Constant[] cs = constants;
|
217 | 224 | size *= 2;
|
| 225 | + // the constant array shall not exceed the size of the constant pool |
| 226 | + size = Math.min(size, CONSTANT_POOL_SIZE); |
218 | 227 | constants = new Constant[size];
|
219 | 228 | System.arraycopy(cs, 0, constants, 0, index);
|
220 | 229 | }
|
|
0 commit comments