Skip to content

Commit bee86d7

Browse files
authored
check whether size has overflowed (#1013)
1 parent a21fe7b commit bee86d7

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/main/java/com/fasterxml/jackson/core/util/TextBuffer.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,11 @@ public String contentsAsString()
452452
if (segLen == 0) { // yup
453453
_resultString = (currLen == 0) ? "" : new String(_currentSegment, 0, currLen);
454454
} else { // no, need to combine
455-
StringBuilder sb = new StringBuilder(segLen + currLen);
455+
final int builderLen = segLen + currLen;
456+
if (builderLen < 0) {
457+
throw new RuntimeException("size cannot be negative (probable int overflow)");
458+
}
459+
StringBuilder sb = new StringBuilder(builderLen);
456460
// First stored segments
457461
if (_segments != null) {
458462
for (int i = 0, len = _segments.size(); i < len; ++i) {
@@ -852,6 +856,9 @@ public char[] finishCurrentSegment() {
852856
_segments.add(_currentSegment);
853857
int oldLen = _currentSegment.length;
854858
_segmentSize += oldLen;
859+
if (_segmentSize < 0) {
860+
throw new RuntimeException("_segmentSize cannot be negative (probable int overflow)");
861+
}
855862
_currentSize = 0;
856863

857864
// Let's grow segments by 50%
@@ -960,6 +967,9 @@ private void expand(int minNewSegmentSize)
960967
_hasSegments = true;
961968
_segments.add(curr);
962969
_segmentSize += curr.length;
970+
if (_segmentSize < 0) {
971+
throw new RuntimeException("_segmentSize cannot be negative (probable int overflow)");
972+
}
963973
_currentSize = 0;
964974
int oldLen = curr.length;
965975

@@ -993,6 +1003,9 @@ private char[] resultArray()
9931003
// nope, not shared
9941004
int size = size();
9951005
if (size < 1) {
1006+
if (size < 0) {
1007+
throw new RuntimeException("size cannot be negative (probable int overflow)");
1008+
}
9961009
return NO_CHARS;
9971010
}
9981011
int offset = 0;

0 commit comments

Comments
 (0)