Calls to discardSomeReadBytes() causes the JsonDecoder to get corrupted
Modification: Added a lastReaderIndex value and if the current readerIndex has been reset, resets the idx and the decoder. Result: Fixes #6156.
This commit is contained in:
parent
b662afeece
commit
231e6a5b7d
@ -45,6 +45,8 @@ public class JsonObjectDecoder extends ByteToMessageDecoder {
|
||||
private int openBraces;
|
||||
private int idx;
|
||||
|
||||
private int lastReaderIndex;
|
||||
|
||||
private int state;
|
||||
private boolean insideString;
|
||||
|
||||
@ -88,6 +90,11 @@ public class JsonObjectDecoder extends ByteToMessageDecoder {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.idx > in.readerIndex() && lastReaderIndex != in.readerIndex()) {
|
||||
this.idx = in.readerIndex();
|
||||
reset();
|
||||
}
|
||||
|
||||
// index of next byte to process.
|
||||
int idx = this.idx;
|
||||
int wrtIdx = in.writerIndex();
|
||||
@ -170,6 +177,7 @@ public class JsonObjectDecoder extends ByteToMessageDecoder {
|
||||
} else {
|
||||
this.idx = idx;
|
||||
}
|
||||
this.lastReaderIndex = in.readerIndex();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -276,4 +276,36 @@ public class JsonObjectDecoderTest {
|
||||
|
||||
assertFalse(ch.finish());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCorruptedFrameException() {
|
||||
final String part1 = "{\"a\":{\"b\":{\"c\":{ \"d\":\"27301\", \"med\":\"d\", \"path\":\"27310\"} }," +
|
||||
" \"status\":\"OK\" } }{\"";
|
||||
final String part2 = "a\":{\"b\":{\"c\":{\"ory\":[{\"competi\":[{\"event\":[{" + "\"externalI\":{\"external\"" +
|
||||
":[{\"id\":\"O\"} ]";
|
||||
|
||||
EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder());
|
||||
|
||||
ByteBuf res;
|
||||
|
||||
ch.writeInbound(Unpooled.copiedBuffer(part1, CharsetUtil.UTF_8));
|
||||
res = ch.readInbound();
|
||||
assertEquals("{\"a\":{\"b\":{\"c\":{ \"d\":\"27301\", \"med\":\"d\", \"path\":\"27310\"} }, " +
|
||||
"\"status\":\"OK\" } }", res.toString(CharsetUtil.UTF_8));
|
||||
res.release();
|
||||
|
||||
ch.writeInbound(Unpooled.copiedBuffer(part2, CharsetUtil.UTF_8));
|
||||
res = ch.readInbound();
|
||||
|
||||
assertNull(res);
|
||||
|
||||
ch.writeInbound(Unpooled.copiedBuffer("}}]}]}]}}}}", CharsetUtil.UTF_8));
|
||||
res = ch.readInbound();
|
||||
|
||||
assertEquals("{\"a\":{\"b\":{\"c\":{\"ory\":[{\"competi\":[{\"event\":[{" + "\"externalI\":{" +
|
||||
"\"external\":[{\"id\":\"O\"} ]}}]}]}]}}}}", res.toString(CharsetUtil.UTF_8));
|
||||
res.release();
|
||||
|
||||
assertFalse(ch.finish());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user