Skip to content

Commit

Permalink
fail if int overflow in _longNameOffset (#1358)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored Nov 9, 2024
1 parent 1e919b3 commit 818647a
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1114,22 +1114,27 @@ private boolean _checkNeedForRehash() {
return false;
}

private int _appendLongName(int[] quads, int qlen)
private int _appendLongName(final int[] quads, final int qlen)
{
int start = _longNameOffset;
final int start = _longNameOffset;
final int newStart = start + qlen;
if (newStart < 0) {
throw new IllegalStateException(String.format(
"Internal error: long name offset overflow; start=%s, qlen=%s", start, qlen));
}

// note: at this point we must already be shared. But may not have enough space
if ((start + qlen) > _hashArea.length) {
// note: at this point we must already be unshared. But may not have enough space
if (newStart > _hashArea.length) {
// try to increment in reasonable chunks; at least space that we need
int toAdd = (start + qlen) - _hashArea.length;
int toAdd = newStart - _hashArea.length;
// but at least 1/8 of regular hash area size or 16kB (whichever smaller)
int minAdd = Math.min(4096, _hashSize);

int newSize = _hashArea.length + Math.max(toAdd, minAdd);
_hashArea = Arrays.copyOf(_hashArea, newSize);
}
System.arraycopy(quads, 0, _hashArea, start, qlen);
_longNameOffset += qlen;
_longNameOffset = newStart;
return start;
}

Expand Down Expand Up @@ -1259,9 +1264,8 @@ private void rehash() throws StreamConstraintsException
final int newSize = oldSize + oldSize;
final int oldEnd = _spilloverEnd;

/* 13-Mar-2010, tatu: Let's guard against OOME that could be caused by
* large documents with unique (or mostly so) names
*/
// 13-Mar-2010, tatu: Let's guard against OOME that could be caused by
// large documents with unique (or mostly so) names
if (newSize > MAX_T_SIZE) {
nukeSymbols(true);
return;
Expand Down

0 comments on commit 818647a

Please sign in to comment.