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:
Andrew McCall 2016-12-23 21:10:26 +00:00 committed by Norman Maurer
parent b662afeece
commit 231e6a5b7d
2 changed files with 40 additions and 0 deletions

View File

@ -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();
}
/**

View File

@ -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());
}
}