Skip to content

Commit a432a82

Browse files
author
Sergey Mashkov
committed
Fix broken readInt reading through the ring buffer edge
Fixes #363
1 parent 6121663 commit a432a82

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

core/kotlinx-coroutines-io/src/main/kotlin/kotlinx/coroutines/experimental/io/ByteBufferChannel.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -811,8 +811,10 @@ internal class ByteBufferChannel(
811811
}
812812

813813
private fun ByteBuffer.rollBytes(n: Int) {
814+
val rem = remaining()
815+
814816
limit(position() + n)
815-
for (i in 1..n - remaining()) {
817+
for (i in 0 until n - rem) {
816818
put(capacity() + ReservedLongIndex + i, get(i))
817819
}
818820
}

core/kotlinx-coroutines-io/src/test/kotlin/kotlinx/coroutines/experimental/io/ByteBufferChannelTest.kt

+35
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,41 @@ class ByteBufferChannelTest : TestBase() {
185185
}
186186
}
187187

188+
@Test
189+
fun testIntEdge2() {
190+
runTest {
191+
for (shift in 1..3) {
192+
for (i in 1..shift) {
193+
ch.writeByte(1)
194+
}
195+
196+
repeat(Size / 4 - 1) {
197+
ch.writeInt(0xeeeeeeeeL)
198+
}
199+
200+
ch.flush()
201+
202+
for (i in 1..shift) {
203+
ch.readByte()
204+
}
205+
206+
ch.writeByte(0x12)
207+
ch.writeByte(0x34)
208+
ch.writeByte(0x56)
209+
ch.writeByte(0x78)
210+
211+
ch.flush()
212+
213+
while (ch.availableForRead > 4) {
214+
ch.readInt()
215+
}
216+
217+
assertEquals(0x12345678, ch.readInt())
218+
}
219+
}
220+
}
221+
222+
188223
@Test
189224
fun testLongB() {
190225
runTest {

0 commit comments

Comments
 (0)