Skip to content

Commit

Permalink
[Java] Fix IndexOutOfBoundsException when new fury deserialize from I…
Browse files Browse the repository at this point in the history
…nputStream (#671)

* ensure buffer size for reading binary from InputStream

* add test
  • Loading branch information
chaokunyang authored Jul 17, 2023
1 parent 165d6e5 commit 325afe3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions java/fury-core/src/main/java/io/fury/Fury.java
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ public Object deserialize(InputStream inputStream, Iterable<MemoryBuffer> outOfB
int read = inputStream.read(buffer.getHeapMemory(), 0, 4);
Preconditions.checkArgument(read == 4);
int size = buffer.readInt();
buffer.ensure(size + 4);
read = inputStream.read(buffer.getHeapMemory(), 4, size);
Preconditions.checkArgument(read == size);
return deserialize(buffer, outOfBandBuffers);
Expand Down Expand Up @@ -1109,6 +1110,7 @@ private Object deserializeFromStream(
int read = inputStream.read(buffer.getHeapMemory(), 0, 4);
Preconditions.checkArgument(read == 4);
int size = buffer.readInt();
buffer.ensure(4 + size);
read = inputStream.read(buffer.getHeapMemory(), 4, size);
Preconditions.checkArgument(read == size);
}
Expand Down
16 changes: 16 additions & 0 deletions java/fury-core/src/test/java/io/fury/FuryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,14 @@ public void testOutputStream() throws IOException {
assertEquals(newObj, beanA);
newObj = fury.deserialize(bis);
assertEquals(newObj, beanA);

fury = Fury.builder().requireClassRegistration(false).build();
// test reader buffer grow
bis = new ByteArrayInputStream(bas.toByteArray());
newObj = fury.deserialize(bis);
assertEquals(newObj, beanA);
newObj = fury.deserialize(bis);
assertEquals(newObj, beanA);
}

@Test
Expand Down Expand Up @@ -465,6 +473,14 @@ public void testJavaOutputStream() throws IOException {
assertEquals(newObj, beanA);
newObj = fury.deserializeJavaObjectAndClass(bis);
assertEquals(newObj, beanA);

fury = Fury.builder().requireClassRegistration(false).build();
// test reader buffer grow
bis = new ByteArrayInputStream(bas.toByteArray());
newObj = fury.deserializeJavaObjectAndClass(bis);
assertEquals(newObj, beanA);
newObj = fury.deserializeJavaObjectAndClass(bis);
assertEquals(newObj, beanA);
}
}
}

0 comments on commit 325afe3

Please sign in to comment.