Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw checked exception if growArray has a size that is too big #1075

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/com/fasterxml/jackson/core/base/ParserBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Arrays;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.io.ContentReference;
import com.fasterxml.jackson.core.io.NumberInput;
Expand Down Expand Up @@ -1553,12 +1554,16 @@ protected ContentReference _contentReferenceRedacted() {
return ContentReference.redacted();
}

protected static int[] growArrayBy(int[] arr, int more)
protected static int[] growArrayBy(int[] arr, int more) throws StreamConstraintsException
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved
{
if (arr == null) {
return new int[more];
}
return Arrays.copyOf(arr, arr.length + more);
final long len = arr.length + more;
if (len > Integer.MAX_VALUE) {
throw new StreamConstraintsException("Unable to grow array to longer to Integer.MAX_VALUE");
}
return Arrays.copyOf(arr, (int) len);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ private final String _parseLongName(int q, final int q2, int q3) throws IOExcept

// Nope, no end in sight. Need to grow quad array etc
if (qlen >= _quadBuffer.length) {
_quadBuffer = _growArrayBy(_quadBuffer, qlen);
_quadBuffer = growArrayBy(_quadBuffer, qlen);
}
_quadBuffer[qlen++] = q;
q = i;
Expand Down Expand Up @@ -1529,7 +1529,7 @@ protected final String parseEscapedName(int[] quads, int qlen, int currQuad, int
// Ok, we'll need room for first byte right away
if (currQuadBytes >= 4) {
if (qlen >= quads.length) {
_quadBuffer = quads = _growArrayBy(quads, quads.length);
_quadBuffer = quads = growArrayBy(quads, quads.length);
}
quads[qlen++] = currQuad;
currQuad = 0;
Expand All @@ -1545,7 +1545,7 @@ protected final String parseEscapedName(int[] quads, int qlen, int currQuad, int
// need room for middle byte?
if (currQuadBytes >= 4) {
if (qlen >= quads.length) {
_quadBuffer = quads = _growArrayBy(quads, quads.length);
_quadBuffer = quads = growArrayBy(quads, quads.length);
}
quads[qlen++] = currQuad;
currQuad = 0;
Expand All @@ -1564,7 +1564,7 @@ protected final String parseEscapedName(int[] quads, int qlen, int currQuad, int
currQuad = (currQuad << 8) | ch;
} else {
if (qlen >= quads.length) {
_quadBuffer = quads = _growArrayBy(quads, quads.length);
_quadBuffer = quads = growArrayBy(quads, quads.length);
}
quads[qlen++] = currQuad;
currQuad = ch;
Expand All @@ -1575,7 +1575,7 @@ protected final String parseEscapedName(int[] quads, int qlen, int currQuad, int

if (currQuadBytes > 0) {
if (qlen >= quads.length) {
_quadBuffer = quads = _growArrayBy(quads, quads.length);
_quadBuffer = quads = growArrayBy(quads, quads.length);
}
quads[qlen++] = pad(currQuad, currQuadBytes);
}
Expand Down Expand Up @@ -1634,7 +1634,7 @@ protected String _handleOddName(int ch) throws IOException
currQuad = (currQuad << 8) | ch;
} else {
if (qlen >= quads.length) {
_quadBuffer = quads = _growArrayBy(quads, quads.length);
_quadBuffer = quads = growArrayBy(quads, quads.length);
}
quads[qlen++] = currQuad;
currQuad = ch;
Expand All @@ -1649,7 +1649,7 @@ protected String _handleOddName(int ch) throws IOException
_nextByte = ch;
if (currQuadBytes > 0) {
if (qlen >= quads.length) {
_quadBuffer = quads = _growArrayBy(quads, quads.length);
_quadBuffer = quads = growArrayBy(quads, quads.length);
}
quads[qlen++] = currQuad;
}
Expand Down Expand Up @@ -1703,7 +1703,7 @@ protected String _parseAposName() throws IOException
// Ok, we'll need room for first byte right away
if (currQuadBytes >= 4) {
if (qlen >= quads.length) {
_quadBuffer = quads = _growArrayBy(quads, quads.length);
_quadBuffer = quads = growArrayBy(quads, quads.length);
}
quads[qlen++] = currQuad;
currQuad = 0;
Expand All @@ -1719,7 +1719,7 @@ protected String _parseAposName() throws IOException
// need room for middle byte?
if (currQuadBytes >= 4) {
if (qlen >= quads.length) {
_quadBuffer = quads = _growArrayBy(quads, quads.length);
_quadBuffer = quads = growArrayBy(quads, quads.length);
}
quads[qlen++] = currQuad;
currQuad = 0;
Expand All @@ -1738,7 +1738,7 @@ protected String _parseAposName() throws IOException
currQuad = (currQuad << 8) | ch;
} else {
if (qlen >= quads.length) {
_quadBuffer = quads = _growArrayBy(quads, quads.length);
_quadBuffer = quads = growArrayBy(quads, quads.length);
}
quads[qlen++] = currQuad;
currQuad = ch;
Expand All @@ -1749,7 +1749,7 @@ protected String _parseAposName() throws IOException

if (currQuadBytes > 0) {
if (qlen >= quads.length) {
_quadBuffer = quads = _growArrayBy(quads, quads.length);
_quadBuffer = quads = growArrayBy(quads, quads.length);
}
quads[qlen++] = pad(currQuad, currQuadBytes);
}
Expand Down Expand Up @@ -1814,7 +1814,7 @@ private final String findName(int[] quads, int qlen, int lastQuad, int lastQuadB
throws JsonParseException, StreamConstraintsException
{
if (qlen >= quads.length) {
_quadBuffer = quads = _growArrayBy(quads, quads.length);
_quadBuffer = quads = growArrayBy(quads, quads.length);
}
quads[qlen++] = pad(lastQuad, lastQuadBytes);
String name = _symbols.findName(quads, qlen);
Expand Down Expand Up @@ -2846,14 +2846,6 @@ private void _reportInvalidOther(int mask)
_reportError("Invalid UTF-8 middle byte 0x"+Integer.toHexString(mask));
}

private static int[] _growArrayBy(int[] arr, int more)
{
if (arr == null) {
return new int[more];
}
return Arrays.copyOf(arr, arr.length + more);
}

/*
/**********************************************************
/* Internal methods, binary access
Expand Down