Skip to content

Commit c499978

Browse files
authored
throw checked exception if growArray has a size that is too big (#1075)
1 parent f34de3d commit c499978

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

src/main/java/com/fasterxml/jackson/core/base/ParserBase.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Arrays;
77

88
import com.fasterxml.jackson.core.*;
9+
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
910
import com.fasterxml.jackson.core.io.IOContext;
1011
import com.fasterxml.jackson.core.io.ContentReference;
1112
import com.fasterxml.jackson.core.io.NumberInput;
@@ -1553,12 +1554,16 @@ protected ContentReference _contentReferenceRedacted() {
15531554
return ContentReference.redacted();
15541555
}
15551556

1556-
protected static int[] growArrayBy(int[] arr, int more)
1557+
protected static int[] growArrayBy(int[] arr, int more) throws StreamConstraintsException
15571558
{
15581559
if (arr == null) {
15591560
return new int[more];
15601561
}
1561-
return Arrays.copyOf(arr, arr.length + more);
1562+
final long len = arr.length + more;
1563+
if (len > Integer.MAX_VALUE) {
1564+
throw new StreamConstraintsException("Unable to grow array to longer to Integer.MAX_VALUE");
1565+
}
1566+
return Arrays.copyOf(arr, (int) len);
15621567
}
15631568

15641569
/*

src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,7 @@ private final String _parseLongName(int q, final int q2, int q3) throws IOExcept
14701470

14711471
// Nope, no end in sight. Need to grow quad array etc
14721472
if (qlen >= _quadBuffer.length) {
1473-
_quadBuffer = _growArrayBy(_quadBuffer, qlen);
1473+
_quadBuffer = growArrayBy(_quadBuffer, qlen);
14741474
}
14751475
_quadBuffer[qlen++] = q;
14761476
q = i;
@@ -1529,7 +1529,7 @@ protected final String parseEscapedName(int[] quads, int qlen, int currQuad, int
15291529
// Ok, we'll need room for first byte right away
15301530
if (currQuadBytes >= 4) {
15311531
if (qlen >= quads.length) {
1532-
_quadBuffer = quads = _growArrayBy(quads, quads.length);
1532+
_quadBuffer = quads = growArrayBy(quads, quads.length);
15331533
}
15341534
quads[qlen++] = currQuad;
15351535
currQuad = 0;
@@ -1545,7 +1545,7 @@ protected final String parseEscapedName(int[] quads, int qlen, int currQuad, int
15451545
// need room for middle byte?
15461546
if (currQuadBytes >= 4) {
15471547
if (qlen >= quads.length) {
1548-
_quadBuffer = quads = _growArrayBy(quads, quads.length);
1548+
_quadBuffer = quads = growArrayBy(quads, quads.length);
15491549
}
15501550
quads[qlen++] = currQuad;
15511551
currQuad = 0;
@@ -1564,7 +1564,7 @@ protected final String parseEscapedName(int[] quads, int qlen, int currQuad, int
15641564
currQuad = (currQuad << 8) | ch;
15651565
} else {
15661566
if (qlen >= quads.length) {
1567-
_quadBuffer = quads = _growArrayBy(quads, quads.length);
1567+
_quadBuffer = quads = growArrayBy(quads, quads.length);
15681568
}
15691569
quads[qlen++] = currQuad;
15701570
currQuad = ch;
@@ -1575,7 +1575,7 @@ protected final String parseEscapedName(int[] quads, int qlen, int currQuad, int
15751575

15761576
if (currQuadBytes > 0) {
15771577
if (qlen >= quads.length) {
1578-
_quadBuffer = quads = _growArrayBy(quads, quads.length);
1578+
_quadBuffer = quads = growArrayBy(quads, quads.length);
15791579
}
15801580
quads[qlen++] = pad(currQuad, currQuadBytes);
15811581
}
@@ -1634,7 +1634,7 @@ protected String _handleOddName(int ch) throws IOException
16341634
currQuad = (currQuad << 8) | ch;
16351635
} else {
16361636
if (qlen >= quads.length) {
1637-
_quadBuffer = quads = _growArrayBy(quads, quads.length);
1637+
_quadBuffer = quads = growArrayBy(quads, quads.length);
16381638
}
16391639
quads[qlen++] = currQuad;
16401640
currQuad = ch;
@@ -1649,7 +1649,7 @@ protected String _handleOddName(int ch) throws IOException
16491649
_nextByte = ch;
16501650
if (currQuadBytes > 0) {
16511651
if (qlen >= quads.length) {
1652-
_quadBuffer = quads = _growArrayBy(quads, quads.length);
1652+
_quadBuffer = quads = growArrayBy(quads, quads.length);
16531653
}
16541654
quads[qlen++] = currQuad;
16551655
}
@@ -1703,7 +1703,7 @@ protected String _parseAposName() throws IOException
17031703
// Ok, we'll need room for first byte right away
17041704
if (currQuadBytes >= 4) {
17051705
if (qlen >= quads.length) {
1706-
_quadBuffer = quads = _growArrayBy(quads, quads.length);
1706+
_quadBuffer = quads = growArrayBy(quads, quads.length);
17071707
}
17081708
quads[qlen++] = currQuad;
17091709
currQuad = 0;
@@ -1719,7 +1719,7 @@ protected String _parseAposName() throws IOException
17191719
// need room for middle byte?
17201720
if (currQuadBytes >= 4) {
17211721
if (qlen >= quads.length) {
1722-
_quadBuffer = quads = _growArrayBy(quads, quads.length);
1722+
_quadBuffer = quads = growArrayBy(quads, quads.length);
17231723
}
17241724
quads[qlen++] = currQuad;
17251725
currQuad = 0;
@@ -1738,7 +1738,7 @@ protected String _parseAposName() throws IOException
17381738
currQuad = (currQuad << 8) | ch;
17391739
} else {
17401740
if (qlen >= quads.length) {
1741-
_quadBuffer = quads = _growArrayBy(quads, quads.length);
1741+
_quadBuffer = quads = growArrayBy(quads, quads.length);
17421742
}
17431743
quads[qlen++] = currQuad;
17441744
currQuad = ch;
@@ -1749,7 +1749,7 @@ protected String _parseAposName() throws IOException
17491749

17501750
if (currQuadBytes > 0) {
17511751
if (qlen >= quads.length) {
1752-
_quadBuffer = quads = _growArrayBy(quads, quads.length);
1752+
_quadBuffer = quads = growArrayBy(quads, quads.length);
17531753
}
17541754
quads[qlen++] = pad(currQuad, currQuadBytes);
17551755
}
@@ -1814,7 +1814,7 @@ private final String findName(int[] quads, int qlen, int lastQuad, int lastQuadB
18141814
throws JsonParseException, StreamConstraintsException
18151815
{
18161816
if (qlen >= quads.length) {
1817-
_quadBuffer = quads = _growArrayBy(quads, quads.length);
1817+
_quadBuffer = quads = growArrayBy(quads, quads.length);
18181818
}
18191819
quads[qlen++] = pad(lastQuad, lastQuadBytes);
18201820
String name = _symbols.findName(quads, qlen);
@@ -2846,14 +2846,6 @@ private void _reportInvalidOther(int mask)
28462846
_reportError("Invalid UTF-8 middle byte 0x"+Integer.toHexString(mask));
28472847
}
28482848

2849-
private static int[] _growArrayBy(int[] arr, int more)
2850-
{
2851-
if (arr == null) {
2852-
return new int[more];
2853-
}
2854-
return Arrays.copyOf(arr, arr.length + more);
2855-
}
2856-
28572849
/*
28582850
/**********************************************************
28592851
/* Internal methods, binary access

0 commit comments

Comments
 (0)