Skip to content

Commit 3aa1ef0

Browse files
committed
Update release notes wrt #1012, minor tweak to error reporting
1 parent bee86d7 commit 3aa1ef0

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ No changes since 2.14
2929
#990: Backport removal of BigDecimal to BigInt conversion (#987)
3030
(contributed by @pjfanning)
3131
#1004: FastDoubleParser license
32+
#1012: Got `NegativeArraySizeException` when calling `writeValueAsString()`
33+
(reported by @klettier)
34+
(fix contributed by @pjfanning)
3235

3336
2.14.2 (28-Jan-2023)
3437

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ public String contentsAsString()
454454
} else { // no, need to combine
455455
final int builderLen = segLen + currLen;
456456
if (builderLen < 0) {
457-
throw new RuntimeException("size cannot be negative (probable int overflow)");
457+
_reportBufferOverflow(segLen, currLen);
458458
}
459459
StringBuilder sb = new StringBuilder(builderLen);
460460
// First stored segments
@@ -857,7 +857,7 @@ public char[] finishCurrentSegment() {
857857
int oldLen = _currentSegment.length;
858858
_segmentSize += oldLen;
859859
if (_segmentSize < 0) {
860-
throw new RuntimeException("_segmentSize cannot be negative (probable int overflow)");
860+
_reportBufferOverflow(_segmentSize, oldLen);
861861
}
862862
_currentSize = 0;
863863

@@ -968,7 +968,7 @@ private void expand(int minNewSegmentSize)
968968
_segments.add(curr);
969969
_segmentSize += curr.length;
970970
if (_segmentSize < 0) {
971-
throw new RuntimeException("_segmentSize cannot be negative (probable int overflow)");
971+
_reportBufferOverflow(_segmentSize - curr.length, curr.length);
972972
}
973973
_currentSize = 0;
974974
int oldLen = curr.length;
@@ -1004,7 +1004,7 @@ private char[] resultArray()
10041004
int size = size();
10051005
if (size < 1) {
10061006
if (size < 0) {
1007-
throw new RuntimeException("size cannot be negative (probable int overflow)");
1007+
_reportBufferOverflow(_segmentSize, _currentSize);
10081008
}
10091009
return NO_CHARS;
10101010
}
@@ -1023,4 +1023,10 @@ private char[] resultArray()
10231023
}
10241024

10251025
private char[] carr(int len) { return new char[len]; }
1026+
1027+
private void _reportBufferOverflow(int prev, int curr) {
1028+
long newSize = (long) prev + (long) curr;
1029+
throw new IllegalStateException("TextBuffer overrun: size reached ("
1030+
+newSize+") exceeds maximum of "+Integer.MAX_VALUE);
1031+
}
10261032
}

0 commit comments

Comments
 (0)