Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc8783ff

Browse files
committed
Reset Utf8ByteBufCharsetDecoder splitCharBuffer,closeAsyncHttpClient#1325
Motivation:Utf8ByteBufCharsetDecoder crashes with BufferOverflow when trying todecode a char whose byte length is larger than the one of the firstsplit char that was decoded.This happens because we only reset position while we should beresetting capacity too.Modifications:User ByteBuffer reset instead os `position(0)`.Result:No more BufferOverflow
1 parent968358a commitc8783ff

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

‎netty-utils/src/main/java/org/asynchttpclient/netty/util/Utf8ByteBufCharsetDecoder.java‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
publicclassUtf8ByteBufCharsetDecoder {
2626

27+
privatestaticfinalintINITIAL_CHAR_BUFFER_SIZE =1024;
28+
privatestaticfinalintUTF_8_MAX_BYTES_PER_CHAR =4;
29+
2730
privatestaticfinalThreadLocal<Utf8ByteBufCharsetDecoder>POOL =newThreadLocal<Utf8ByteBufCharsetDecoder>() {
2831
protectedUtf8ByteBufCharsetDecoderinitialValue() {
2932
returnnewUtf8ByteBufCharsetDecoder();
@@ -45,15 +48,8 @@ public static String decodeUtf8(ByteBuf... bufs) throws CharacterCodingException
4548
}
4649

4750
privatefinalCharsetDecoderdecoder =UTF_8.newDecoder();
48-
protectedCharBuffercharBuffer =allocateCharBuffer(1024);
49-
privateByteBuffersplitCharBuffer;
50-
51-
protectedvoidinitSplitCharBuffer() {
52-
if (splitCharBuffer ==null) {
53-
// UTF-8 chars are 4 bytes max
54-
splitCharBuffer =ByteBuffer.allocate(4);
55-
}
56-
}
51+
protectedCharBuffercharBuffer =allocateCharBuffer(INITIAL_CHAR_BUFFER_SIZE);
52+
privateByteBuffersplitCharBuffer =ByteBuffer.allocate(UTF_8_MAX_BYTES_PER_CHAR);
5753

5854
protectedCharBufferallocateCharBuffer(intl) {
5955
returnCharBuffer.allocate(l);
@@ -75,6 +71,7 @@ private void ensureCapacity(int l) {
7571
publicvoidreset() {
7672
decoder.reset();
7773
charBuffer.clear();
74+
splitCharBuffer.clear();
7875
}
7976

8077
privatestaticintcharSize(bytefirstByte)throwsCharacterCodingException {
@@ -119,8 +116,6 @@ private void handleSplitCharBuffer(ByteBuffer nioBuffer, boolean endOfInput) thr
119116
if (res.isError()) {
120117
res.throwException();
121118
}
122-
123-
splitCharBuffer.position(0);
124119
}
125120
}
126121

@@ -135,7 +130,6 @@ protected void decodePartial(ByteBuffer nioBuffer, boolean endOfInput) throws Ch
135130
CoderResultres =decoder.decode(nioBuffer,charBuffer,endOfInput);
136131
if (res.isUnderflow()) {
137132
if (nioBuffer.remaining() >0) {
138-
initSplitCharBuffer();
139133
splitCharBuffer.put(nioBuffer);
140134
}
141135
}elseif (res.isError()) {

‎netty-utils/src/test/java/org/asynchttpclient/netty/util/ByteBufUtilsTest.java‎

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
packageorg.asynchttpclient.netty.util;
1515

16-
importstaticjava.nio.charset.StandardCharsets.US_ASCII;
16+
importstaticjava.nio.charset.StandardCharsets.*;
1717
importstaticorg.testng.Assert.assertEquals;
1818
importio.netty.buffer.ByteBuf;
1919
importio.netty.buffer.Unpooled;
@@ -25,17 +25,43 @@ public class ByteBufUtilsTest {
2525
@Test
2626
publicvoidtestByteBuf2BytesHasBackingArray() {
2727
byte[]inputBytes ="testdata".getBytes(US_ASCII);
28-
ByteBufinputBuf =Unpooled.wrappedBuffer(inputBytes);
29-
byte[]output =ByteBufUtils.byteBuf2Bytes(inputBuf);
30-
assertEquals(output,inputBytes);
28+
ByteBufbuf =Unpooled.wrappedBuffer(inputBytes);
29+
try {
30+
byte[]output =ByteBufUtils.byteBuf2Bytes(buf);
31+
assertEquals(output,inputBytes);
32+
}finally {
33+
buf.release();
34+
}
3135
}
3236

3337
@Test
3438
publicvoidtestByteBuf2BytesNoBackingArray() {
3539
byte[]inputBytes ="testdata".getBytes(US_ASCII);
36-
ByteBufinputBuf =Unpooled.directBuffer();
37-
inputBuf.writeBytes(inputBytes);
38-
byte[]output =ByteBufUtils.byteBuf2Bytes(inputBuf);
39-
assertEquals(output,inputBytes);
40+
ByteBufbuf =Unpooled.directBuffer();
41+
try {
42+
buf.writeBytes(inputBytes);
43+
byte[]output =ByteBufUtils.byteBuf2Bytes(buf);
44+
assertEquals(output,inputBytes);
45+
}finally {
46+
buf.release();
47+
}
48+
}
49+
50+
@Test
51+
publicvoidbyteBufs2StringShouldBeAbleToDealWithCharsWithVariableBytesLength()throwsException {
52+
StringinputString ="°ä–";
53+
byte[]inputBytes =inputString.getBytes(UTF_8);
54+
55+
for (inti =1;i <inputBytes.length -1;i++) {
56+
ByteBufbuf1 =Unpooled.wrappedBuffer(inputBytes,0,i);
57+
ByteBufbuf2 =Unpooled.wrappedBuffer(inputBytes,i,inputBytes.length -i);
58+
try {
59+
Strings =ByteBufUtils.byteBuf2String(UTF_8,buf1,buf2);
60+
assertEquals(s,inputString);
61+
}finally {
62+
buf1.release();
63+
buf2.release();
64+
}
65+
}
4066
}
4167
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp