From b093ecae0473d398df899b2f45e39ef5c7013099 Mon Sep 17 00:00:00 2001 From: Thomas Heigl Date: Wed, 4 Aug 2021 19:50:41 +0200 Subject: [PATCH] #834 Support skipping input chunks after a buffer underflow --- .../kryo/io/InputChunked.java | 1 + .../esotericsoftware/kryo/io/ChunkedTest.java | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/com/esotericsoftware/kryo/io/InputChunked.java b/src/com/esotericsoftware/kryo/io/InputChunked.java index 2b67e46e4..53aff1ecc 100644 --- a/src/com/esotericsoftware/kryo/io/InputChunked.java +++ b/src/com/esotericsoftware/kryo/io/InputChunked.java @@ -99,6 +99,7 @@ private boolean readChunkSize () { /** Advances the stream to the next chunk. InputChunked will appear to hit the end of the data until this method is called. */ public void nextChunk () { + position = limit; // Underflow resets the position to 0. Ensure we are at the end of the chunk. if (chunkSize == -1) readChunkSize(); // No current chunk, expect a new chunk. while (chunkSize > 0) skip(chunkSize); diff --git a/test/com/esotericsoftware/kryo/io/ChunkedTest.java b/test/com/esotericsoftware/kryo/io/ChunkedTest.java index c31ef1f16..b0a5a02e6 100644 --- a/test/com/esotericsoftware/kryo/io/ChunkedTest.java +++ b/test/com/esotericsoftware/kryo/io/ChunkedTest.java @@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.*; +import com.esotericsoftware.kryo.KryoException; import org.junit.jupiter.api.Test; /** @author Nathan Sweet */ @@ -56,4 +57,27 @@ void testChunks () { assertEquals(5678, input.readInt()); input.close(); } + + @Test + void testSkipAfterUnderFlow () { + Output output = new Output(512); + OutputChunked outputChunked = new OutputChunked(output); + outputChunked.writeInt(1); + outputChunked.endChunk(); + outputChunked.writeInt(2); + outputChunked.endChunk(); + output.close(); + + Input input = new Input(output.getBuffer()); + InputChunked inputChunked = new InputChunked(input); + assertEquals(1, inputChunked.readInt()); + try { + inputChunked.readInt(); + } catch (KryoException ex) { + // underflow is desired + } + inputChunked.nextChunk(); + assertEquals(2, inputChunked.readInt()); + input.close(); + } }