Skip to content

Commit 67dd3f7

Browse files
JoeWang-Javaslowhog
authored andcommittedJul 19, 2022
8272249: Better properties of loaded Properties
Reviewed-by: naoto, lancea, rhalade, mschoene
1 parent 243c76f commit 67dd3f7

File tree

1 file changed

+39
-19
lines changed
  • src/java.base/share/classes/jdk/internal/util/xml/impl

1 file changed

+39
-19
lines changed
 

‎src/java.base/share/classes/jdk/internal/util/xml/impl/Parser.java

+39-19
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.InputStreamReader;
3131
import java.io.Reader;
3232
import java.io.UnsupportedEncodingException;
33+
import java.util.Arrays;
3334
import java.util.HashMap;
3435
import java.util.Map;
3536
import jdk.internal.org.xml.sax.InputSource;
@@ -42,7 +43,11 @@ public abstract class Parser {
4243

4344
public static final String FAULT = "";
4445
protected static final int BUFFSIZE_READER = 512;
46+
// Initial buffer (mBuff) size
4547
protected static final int BUFFSIZE_PARSER = 128;
48+
// Max buffer size
49+
private static final int MAX_ARRAY_SIZE = 1024 << 16;
50+
4651
/**
4752
* The end of stream character.
4853
*/
@@ -525,6 +530,10 @@ private void dtd() throws Exception {
525530
mPh = PH_DTD; // DTD
526531
for (short st = 0; st >= 0;) {
527532
ch = getch();
533+
// report error if EOS is reached while parsing the DTD
534+
if (ch == EOS) {
535+
panic(FAULT);
536+
}
528537
switch (st) {
529538
case 0: // read the document type name
530539
if (chtyp(ch) != ' ') {
@@ -1664,6 +1673,10 @@ private void cdat()
16641673
mBuffIdx = -1;
16651674
for (short st = 0; st >= 0;) {
16661675
ch = getch();
1676+
// report error if EOS is reached while parsing the DTD
1677+
if (ch == EOS) {
1678+
panic(FAULT);
1679+
}
16671680
switch (st) {
16681681
case 0: // the first '[' of the CDATA open
16691682
if (ch == '[') {
@@ -2525,7 +2538,7 @@ private char bkeyword()
25252538
}
25262539

25272540
/**
2528-
* Reads a single or double quotted string in to the buffer.
2541+
* Reads a single or double quoted string into the buffer.
25292542
*
25302543
* This method resolves entities inside a string unless the parser parses
25312544
* DTD.
@@ -2660,7 +2673,7 @@ protected abstract void bflash_ws()
26602673
* @param ch The character to append to the buffer.
26612674
* @param mode The normalization mode.
26622675
*/
2663-
private void bappend(char ch, char mode) {
2676+
private void bappend(char ch, char mode) throws Exception {
26642677
// This implements attribute value normalization as
26652678
// described in the XML specification [#3.3.3].
26662679
switch (mode) {
@@ -2710,16 +2723,9 @@ private void bappend(char ch, char mode) {
27102723
*
27112724
* @param ch The character to append to the buffer.
27122725
*/
2713-
private void bappend(char ch) {
2714-
try {
2715-
mBuff[++mBuffIdx] = ch;
2716-
} catch (Exception exp) {
2717-
// Double the buffer size
2718-
char buff[] = new char[mBuff.length << 1];
2719-
System.arraycopy(mBuff, 0, buff, 0, mBuff.length);
2720-
mBuff = buff;
2721-
mBuff[mBuffIdx] = ch;
2722-
}
2726+
private void bappend(char ch) throws Exception {
2727+
ensureCapacity(++mBuffIdx);
2728+
mBuff[mBuffIdx] = ch;
27232729
}
27242730

27252731
/**
@@ -2729,14 +2735,9 @@ private void bappend(char ch) {
27292735
* @param cidx The character buffer (mChars) start index.
27302736
* @param bidx The parser buffer (mBuff) start index.
27312737
*/
2732-
private void bcopy(int cidx, int bidx) {
2738+
private void bcopy(int cidx, int bidx) throws Exception {
27332739
int length = mChIdx - cidx;
2734-
if ((bidx + length + 1) >= mBuff.length) {
2735-
// Expand the buffer
2736-
char buff[] = new char[mBuff.length + length];
2737-
System.arraycopy(mBuff, 0, buff, 0, mBuff.length);
2738-
mBuff = buff;
2739-
}
2740+
ensureCapacity(bidx + length + 1);
27402741
System.arraycopy(mChars, cidx, mBuff, bidx, length);
27412742
mBuffIdx += length;
27422743
}
@@ -3429,4 +3430,23 @@ protected Pair del(Pair pair) {
34293430

34303431
return next;
34313432
}
3433+
3434+
private void ensureCapacity(int minCapacity) throws Exception {
3435+
if (mBuff == null) {
3436+
int newCapacity = minCapacity > BUFFSIZE_PARSER ?
3437+
minCapacity + BUFFSIZE_PARSER : BUFFSIZE_PARSER;
3438+
mBuff = new char[newCapacity];
3439+
return;
3440+
}
3441+
3442+
if (mBuff.length <= minCapacity) {
3443+
int size = mBuff.length << 1;
3444+
int newCapacity = size > minCapacity ? size : minCapacity + BUFFSIZE_PARSER;
3445+
if (newCapacity < 0 || newCapacity > MAX_ARRAY_SIZE) {
3446+
panic(FAULT);
3447+
}
3448+
3449+
mBuff = Arrays.copyOf(mBuff, newCapacity);
3450+
}
3451+
}
34323452
}

0 commit comments

Comments
 (0)