Skip to content

Commit 01b974c

Browse files
committed
Fixed bitmasking error
For large message sizes whenever the header was split up between packets there was a bitmasking error that calculated the chunksize wrong.
1 parent 12dfe1a commit 01b974c

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

driver/src/main/java/org/neo4j/driver/internal/connector/socket/BufferingChunkedInput.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ public BufferingChunkedInput( ReadableByteChannel channel, int bufferCapacity )
8888
this.state = State.AWAITING_CHUNK;
8989
}
9090

91+
/*
92+
* Use only in tests
93+
*/
94+
int remainingChunkSize()
95+
{
96+
return remainingChunkSize;
97+
}
98+
9199
/**
92100
* Internal state machine used for reading data from the channel into the buffer.
93101
*/
@@ -220,7 +228,7 @@ public State readChunkSize( BufferingChunkedInput ctx ) throws IOException
220228
{
221229
//Now we have enough space to read the rest of the chunk size
222230
byte partialChunkSize = ctx.buffer.get();
223-
ctx.remainingChunkSize = (ctx.remainingChunkSize | partialChunkSize) & 0xFFFF;
231+
ctx.remainingChunkSize = ctx.remainingChunkSize | (partialChunkSize & 0xFF);
224232
return IN_CHUNK;
225233
}
226234
else

driver/src/test/java/org/neo4j/driver/internal/connector/socket/BufferingChunkedInputTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ public void shouldReadOneByteWhenSplitHeader() throws IOException
9292
assertThat( b2, equalTo( (byte) 37 ) );
9393
}
9494

95+
@Test
96+
public void shouldReadChunkWithSplitHeaderForBigMessages() throws IOException
97+
{
98+
// Given
99+
int packetSize = 384;
100+
BufferingChunkedInput input =
101+
new BufferingChunkedInput( packets( packet( 1 ), packet( -128 ), fillPacket( packetSize, 1 ) ) );
102+
103+
// Then
104+
assertThat( input.readByte(), equalTo( (byte) 1 ) );
105+
assertThat( input.remainingChunkSize(), equalTo( packetSize - 1 ) );
106+
107+
for ( int i = 1; i < 384; i++ )
108+
{
109+
assertThat( input.readByte(), equalTo( (byte) 1 ) );
110+
}
111+
assertThat( input.remainingChunkSize(), equalTo( 0 ) );
112+
}
113+
95114
@Test
96115
public void shouldReadOneByteInOneChunkWhenBustingBuffer() throws IOException
97116
{
@@ -415,7 +434,7 @@ public void close() throws IOException
415434
BufferingChunkedInput input = new BufferingChunkedInput( channel );
416435

417436
// Then
418-
assertThat(input.readByte(), equalTo( (byte)11 ));
437+
assertThat( input.readByte(), equalTo( (byte) 11 ) );
419438

420439
}
421440

@@ -435,6 +454,17 @@ public void shouldFailNicelyOnClosedConnections() throws IOException
435454
input.readByte();
436455
}
437456

457+
private ReadableByteChannel fillPacket( int size, int value )
458+
{
459+
int[] ints = new int[size];
460+
for ( int i = 0; i < size; i++ )
461+
{
462+
ints[i] = value;
463+
}
464+
465+
return packet( ints );
466+
}
467+
438468
private ReadableByteChannel packet( int... bytes )
439469
{
440470
byte[] byteArray = new byte[bytes.length];

0 commit comments

Comments
 (0)