Skip to content

Commit 154292b

Browse files
Fix RecyclerBytesStreamOutput allocating unlimited heap for some capacities (#90632) (#90636)
We'd allocate infinite memory here because we'd overflow to negative capacities for newPosition close to `Integer.MAX_VALUE`.
1 parent a3d8610 commit 154292b

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

docs/changelog/90632.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 90632
2+
summary: Fix `RecyclerBytesStreamOutput` allocating unlimited heap for some capacities
3+
area: Network
4+
type: bug
5+
issues: []

server/src/main/java/org/elasticsearch/common/io/stream/RecyclerBytesStreamOutput.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,12 @@ private void ensureCapacity(int bytesNeeded) {
225225
}
226226

227227
private void ensureCapacityFromPosition(long newPosition) {
228+
// Integer.MAX_VALUE is not a multiple of the page size so we can only allocate the largest multiple of the pagesize that is less
229+
// than Integer.MAX_VALUE
230+
if (newPosition > Integer.MAX_VALUE - (Integer.MAX_VALUE % pageSize)) {
231+
throw new IllegalArgumentException(getClass().getSimpleName() + " cannot hold more than 2GB of data");
232+
}
228233
while (newPosition > currentCapacity) {
229-
if (newPosition > Integer.MAX_VALUE) {
230-
throw new IllegalArgumentException(getClass().getSimpleName() + " cannot hold more than 2GB of data");
231-
}
232234
Recycler.V<BytesRef> newPage = recycler.obtain();
233235
assert pageSize == newPage.v().length;
234236
pages.add(newPage);

0 commit comments

Comments
 (0)