Skip to content

Commit 05c101c

Browse files
committed
Sync ProtonJ2 with upstream
1 parent c35dfe3 commit 05c101c

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/main/qpid/org/apache/qpid/protonj2/codec/decoders/ProtonDecoderState.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,13 @@ public String decodeUTF8(ProtonBuffer buffer, int length) throws DecodeException
104104
private static String internalDecode(ProtonBuffer buffer, final int length, CharsetDecoder decoder, char[] scratch) {
105105
final int bufferInitialPosition = buffer.getReadOffset();
106106

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++) {
109114
final byte b = buffer.getByte(bufferInitialPosition + offset);
110115
if (b < 0) {
111116
break;
@@ -124,14 +129,24 @@ private static String internalDecode(ProtonBuffer buffer, final int length, Char
124129

125130
private static String internalDecodeUTF8(final ProtonBuffer buffer, final int length, final char[] chars, final int offset, final CharsetDecoder decoder) {
126131
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+
127142
out.position(offset);
128143

129144
// Create a buffer from the remaining portion of the buffer and then use the decoder to complete the work
130145
// 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);
132147

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);
135150

136151
try {
137152
for (;;) {

0 commit comments

Comments
 (0)