@@ -452,7 +452,11 @@ public String contentsAsString()
452
452
if (segLen == 0 ) { // yup
453
453
_resultString = (currLen == 0 ) ? "" : new String (_currentSegment , 0 , currLen );
454
454
} 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 );
456
460
// First stored segments
457
461
if (_segments != null ) {
458
462
for (int i = 0 , len = _segments .size (); i < len ; ++i ) {
@@ -852,6 +856,9 @@ public char[] finishCurrentSegment() {
852
856
_segments .add (_currentSegment );
853
857
int oldLen = _currentSegment .length ;
854
858
_segmentSize += oldLen ;
859
+ if (_segmentSize < 0 ) {
860
+ throw new RuntimeException ("_segmentSize cannot be negative (probable int overflow)" );
861
+ }
855
862
_currentSize = 0 ;
856
863
857
864
// Let's grow segments by 50%
@@ -960,6 +967,9 @@ private void expand(int minNewSegmentSize)
960
967
_hasSegments = true ;
961
968
_segments .add (curr );
962
969
_segmentSize += curr .length ;
970
+ if (_segmentSize < 0 ) {
971
+ throw new RuntimeException ("_segmentSize cannot be negative (probable int overflow)" );
972
+ }
963
973
_currentSize = 0 ;
964
974
int oldLen = curr .length ;
965
975
@@ -993,6 +1003,9 @@ private char[] resultArray()
993
1003
// nope, not shared
994
1004
int size = size ();
995
1005
if (size < 1 ) {
1006
+ if (size < 0 ) {
1007
+ throw new RuntimeException ("size cannot be negative (probable int overflow)" );
1008
+ }
996
1009
return NO_CHARS ;
997
1010
}
998
1011
int offset = 0 ;
0 commit comments