@@ -104,8 +104,13 @@ public String decodeUTF8(ProtonBuffer buffer, int length) throws DecodeException
104
104
private static String internalDecode (ProtonBuffer buffer , final int length , CharsetDecoder decoder , char [] scratch ) {
105
105
final int bufferInitialPosition = buffer .getReadOffset ();
106
106
107
- int offset ;
108
- for (offset = 0 ; offset < length ; offset ++) {
107
+ if (length < 0 ) {
108
+ throw new IllegalArgumentException ("Specified UTF length:" + length + " cannot be negative." );
109
+ }
110
+
111
+ int offset = 0 ;
112
+
113
+ for (; offset < length ; offset ++) {
109
114
final byte b = buffer .getByte (bufferInitialPosition + offset );
110
115
if (b < 0 ) {
111
116
break ;
@@ -124,14 +129,24 @@ private static String internalDecode(ProtonBuffer buffer, final int length, Char
124
129
125
130
private static String internalDecodeUTF8 (final ProtonBuffer buffer , final int length , final char [] chars , final int offset , final CharsetDecoder decoder ) {
126
131
final CharBuffer out = CharBuffer .wrap (chars );
132
+ final int remaining = length - offset ;
133
+
134
+ if (offset < 0 ) {
135
+ throw new IllegalArgumentException ("Specified offset:" + offset + " cannot be negative." );
136
+ }
137
+
138
+ if (remaining < 0 ) {
139
+ throw new IllegalArgumentException ("Remaining UTF8 Bytes size cannot be negative, was " + remaining );
140
+ }
141
+
127
142
out .position (offset );
128
143
129
144
// Create a buffer from the remaining portion of the buffer and then use the decoder to complete the work
130
145
// remember to move the main buffer position to consume the data processed.
131
- ByteBuffer byteBuffer = ByteBuffer .allocate (length - offset );
146
+ ByteBuffer byteBuffer = ByteBuffer .allocate (remaining );
132
147
133
- buffer .copyInto (buffer .getReadOffset (), byteBuffer , 0 , length - offset );
134
- buffer .advanceReadOffset (length - offset );
148
+ buffer .copyInto (buffer .getReadOffset (), byteBuffer , 0 , remaining );
149
+ buffer .advanceReadOffset (remaining );
135
150
136
151
try {
137
152
for (;;) {
0 commit comments