You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When using ByteArrayIndexInput::readBytes on a sliced ByteArrayIndexInput, the position validation check seems to be incorrect and allows reading beyond the valid boundary of the slice.
This is due to incorrect validation check in ByteArrayIndexInput::readBytes() method -
if (pos + len > this.offset + length) {
throw new EOFException("seek past EOF");
}
The above code pointer exists since a very long time. The scenario of readBytes() called on a slice was not covered in a UT and hence this may have been missed. Please let me know if this does not seem to be a bug and rather is some misunderstanding wrt how slices are supposed to work. If this looks legit, I'll raise a PR with the fix and UT.
To Reproduce
Below is a UT that can be added to ByteArrayIndexInputTests to reproduce the behaviour. The UT will fail for the current implementation of readBytes().
public void testReadBytesWithSlice() throws IOException {
int inputLength = randomIntBetween(100, 1000);
byte[] input = randomUnicodeOfLength(inputLength).getBytes(StandardCharsets.UTF_8);
IndexInput indexInput = getIndexInput(input);
int sliceOffset = randomIntBetween(1, inputLength - 10);
int sliceLength = randomIntBetween(1, inputLength - 10 - sliceOffset);
IndexInput slice = indexInput.slice("slice", sliceOffset, sliceLength);
// read a byte from sliced index input and verify if the read value is correct
assertEquals(input[sliceOffset], slice.readByte());
// read few more bytes into a byte array
int bytesToRead = randomIntBetween(1, sliceLength - 1);
slice.readBytes(new byte[bytesToRead], 0, bytesToRead);
// now try to read beyond the boundary of the slice, but within the
// boundary of the original IndexInput. We've already read few bytes
// so this is expected to fail
assertThrows(EOFException.class, () -> slice.readBytes(new byte[sliceLength], 0, sliceLength));
}
Expected behavior ByteArrayIndexInput::readBytes should not allow reads beyond the boundary of the slice and throw an EOFException.
Proposal is to fix the validation check as below -
if (pos + len > length) {
throw new EOFException("seek past EOF");
}
Additional context
Attaching a manual dry-run example below -
We consider a basic scenario where we have a slice of length 4 and per the current implementation can call readBytes with len = 5 without raising an EOFException.
Describe the bug
When using
ByteArrayIndexInput::readBytes
on a slicedByteArrayIndexInput
, the position validation check seems to be incorrect and allows reading beyond the valid boundary of the slice.This is due to incorrect validation check in
ByteArrayIndexInput::readBytes()
method -The above code pointer exists since a very long time. The scenario of
readBytes()
called on a slice was not covered in a UT and hence this may have been missed. Please let me know if this does not seem to be a bug and rather is some misunderstanding wrt how slices are supposed to work. If this looks legit, I'll raise a PR with the fix and UT.To Reproduce
Below is a UT that can be added to
ByteArrayIndexInputTests
to reproduce the behaviour. The UT will fail for the current implementation ofreadBytes()
.Expected behavior
ByteArrayIndexInput::readBytes
should not allow reads beyond the boundary of the slice and throw anEOFException
.Proposal is to fix the validation check as below -
Additional context
Attaching a manual dry-run example below -
We consider a basic scenario where we have a slice of length
4
and per the current implementation can callreadBytes
withlen = 5
without raising an EOFException.The text was updated successfully, but these errors were encountered: