Skip to content

Commit b2644ff

Browse files
samyronbyroot
authored andcommitted
Handle the case were the capacity overflows Integer.MAX_VALUE.
1 parent 5274d5d commit b2644ff

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

java/src/json/ext/LinkedSegmentedByteListDirectOutputStream.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ public void write(int b) throws IOException {
5858
if (this.length + 1 < 0) {
5959
throw new IOException("Total length exceeds maximum length of an array.");
6060
}
61-
if (c.next == null) {
62-
numSegments++;
63-
c.next = new Segment(c.buffer.length * 2);
61+
numSegments++;
62+
int capacity = c.buffer.length * 2;
63+
if (capacity < 0) {
64+
capacity = Integer.MAX_VALUE - length;
6465
}
66+
c.next = new Segment(capacity);
6567
c = c.next;
6668
current = c;
6769
}
@@ -81,10 +83,12 @@ public void write(byte[] bytes, int start, int length) throws IOException {
8183
if (this.length + remaining < 0) {
8284
throw new IOException("Total length exceeds maximum length of an array.");
8385
}
84-
if (c.next == null) {
85-
numSegments++;
86-
c.next = new Segment(c.buffer.length * 2);
86+
numSegments++;
87+
int capacity = c.buffer.length * 2;
88+
if (capacity < 0) {
89+
capacity = Integer.MAX_VALUE - length;
8790
}
91+
c.next = new Segment(capacity);
8892
c = c.next;
8993
current = c;
9094
}

java/src/json/ext/SegmentedByteListDirectOutputStream.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public void write(int b) throws IOException {
4040
}
4141
currentSegmentIndex++;
4242
int capacity = currentSegment.length * 2;
43+
if (capacity < 0) {
44+
capacity = Integer.MAX_VALUE - totalLength;
45+
}
4346
currentSegment = new byte[capacity];
4447
currentSegmentLength = 0;
4548
segments[currentSegmentIndex] = currentSegment;
@@ -59,6 +62,9 @@ public void write(byte[] bytes, int start, int length) throws IOException {
5962
}
6063
currentSegmentIndex++;
6164
int capacity = currentSegment.length * 2;
65+
if (capacity < 0) {
66+
capacity = Integer.MAX_VALUE - totalLength;
67+
}
6268
currentSegment = new byte[capacity];
6369
currentSegmentLength = 0;
6470
segments[currentSegmentIndex] = currentSegment;

0 commit comments

Comments
 (0)