Skip to content

Commit

Permalink
Ensures that the binary reader can handle a ByteArrayInputStream that…
Browse files Browse the repository at this point in the history
… returns a negative number from available().
  • Loading branch information
tgregg committed Oct 24, 2023
1 parent cf6ef90 commit 40ffea2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/com/amazon/ion/impl/IonCursorBinary.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,10 @@ private static IonBufferConfiguration getFixedSizeConfigurationFor(
ByteArrayInputStream inputStream,
int alreadyReadLen
) {
int fixedBufferSize = inputStream.available();
// Note: ByteArrayInputStream.available() can return a negative number because its constructor does
// not validate that the offset and length provided are actually within range of the provided byte array.
// Setting the result to 0 in this case avoids an error when looking up the fixed sized configuration.
int fixedBufferSize = Math.max(0, inputStream.available());
if (alreadyReadLen > 0) {
fixedBufferSize += alreadyReadLen;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3321,6 +3321,17 @@ public void concatenatedAfterMissingGZIPTrailer() throws Exception {
assertEquals(1, oversizedCounter.get());
}

@Test
public void shouldNotFailWhenProvidedWithAnEmptyByteArrayInputStream() throws Exception {
reader = IonReaderBuilder.standard().build(new ByteArrayInputStream(new byte[]{}));
assertSequence(next(null));
reader.close();
// The following ByteArrayInputStream is weird, but not disallowed. Its available() method will return -1.
reader = IonReaderBuilder.standard().build(new ByteArrayInputStream(new byte[]{}, 1, 1));
assertSequence(next(null));
reader.close();
}

@ParameterizedTest(name = "constructFromBytes={0}")
@ValueSource(booleans = {true, false})
public void incompleteContainerNonContinuable(boolean constructFromBytes) throws Exception {
Expand Down

0 comments on commit 40ffea2

Please sign in to comment.