Skip to content

Commit

Permalink
Fixes ByteArrayIndexInput::validatePos
Browse files Browse the repository at this point in the history
Signed-off-by: Paras Jain <parasjaz@amazon.com>
  • Loading branch information
Paras Jain authored and andrross committed Nov 14, 2023
1 parent 3f30511 commit 78ade58
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix passing wrong parameter when calling newConfigurationException() in DotExpanderProcessor ([#10737](https://github.com/opensearch-project/OpenSearch/pull/10737))
- Fix SuggestSearch.testSkipDuplicates by forceing refresh when indexing its test documents ([#11068](https://github.com/opensearch-project/OpenSearch/pull/11068))
- Adding version condition while adding geoshape doc values to the index, to ensure backward compatibility.([#11095](https://github.com/opensearch-project/OpenSearch/pull/11095))
- Fix ByteArrayIndexInput::validatePos ([#10481](https://github.com/opensearch-project/OpenSearch/issues/10481))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public long internalReadLong(long pos) {
}

private void validatePos(long pos, int len) throws EOFException {
if (pos < 0 || pos + len > length + offset) {
if (pos < 0 || pos + len > length) {
throw new EOFException("seek past EOF");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

package org.opensearch.common.lucene.store;

import org.apache.lucene.store.IndexInput;

import java.io.EOFException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -153,4 +155,37 @@ public void testRandomAccessReads() throws IOException {
// 10001001 00100101 10001001 00110000 11100111 00100100 10110001 00101110
assertEquals(-8564288273245753042L, indexInput.readLong(1));
}


public void testReadBytesWithSlice() throws IOException {
int inputLength = randomIntBetween(100, 1000);

byte[] input = randomUnicodeOfLength(inputLength).getBytes(StandardCharsets.UTF_8);
ByteArrayIndexInput indexInput = new ByteArrayIndexInput("test", input);

int sliceOffset = randomIntBetween(1, inputLength - 10);
int sliceLength = randomIntBetween(2, inputLength - 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));

// seek to EOF and then try to read
slice.seek(sliceLength);
assertThrows(EOFException.class, () -> slice.readBytes(new byte[1], 0, 1));

slice.close();
indexInput.close();

}

}

0 comments on commit 78ade58

Please sign in to comment.