Skip to content

Commit

Permalink
Guard against overflow when expanding sort buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshRosen committed May 12, 2015
1 parent 85da63f commit fdcac08
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public UnsafeShuffleInMemorySorter(int initialSize) {

public void expandSortBuffer() {
final long[] oldBuffer = sortBuffer;
sortBuffer = new long[oldBuffer.length * 2];
// Guard against overflow:
final int newLength = oldBuffer.length * 2 > 0 ? (oldBuffer.length * 2) : Integer.MAX_VALUE;
sortBuffer = new long[newLength];
System.arraycopy(oldBuffer, 0, sortBuffer, 0, oldBuffer.length);
}

Expand All @@ -71,7 +73,11 @@ public long getMemoryUsage() {
*/
public void insertRecord(long recordPointer, int partitionId) {
if (!hasSpaceForAnotherRecord()) {
expandSortBuffer();
if (sortBuffer.length == Integer.MAX_VALUE) {
throw new IllegalStateException("Sort buffer has reached maximum size");
} else {
expandSortBuffer();
}
}
sortBuffer[sortBufferInsertPosition] =
PackedRecordPointer.packPointer(recordPointer, partitionId);
Expand Down

0 comments on commit fdcac08

Please sign in to comment.