Skip to content

Commit

Permalink
Revert "Fix bug in SlicedInputStream with zero length (opensearch-pro…
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianli Feng committed Nov 3, 2022
1 parent 3bc181f commit b7814c9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 61 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Better plural stemmer than minimal_english ([#4738](https://github.com/opensearch-project/OpenSearch/pull/4738))
- Fix AbstractStringFieldDataTestCase tests to account for TotalHits lower bound ([4867](https://github.com/opensearch-project/OpenSearch/pull/4867))
- [Segment Replication] Fix bug of replica shard's translog not purging on index flush when segment replication is enabled ([4975](https://github.com/opensearch-project/OpenSearch/pull/4975))
- Fix bug in SlicedInputStream with zero length ([#4863](https://github.com/opensearch-project/OpenSearch/pull/4863))
-

### Security
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

/**
* A {@link SlicedInputStream} is a logical
Expand Down Expand Up @@ -101,11 +100,6 @@ public final int read() throws IOException {

@Override
public final int read(byte[] buffer, int offset, int length) throws IOException {
Objects.checkFromIndexSize(offset, length, buffer.length);
if (length == 0) {
return 0;
}

final InputStream stream = currentStream();
if (stream == null) {
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,13 @@
package org.opensearch.index.snapshots.blobstore;

import com.carrotsearch.randomizedtesting.generators.RandomNumbers;

import org.hamcrest.MatcherAssert;
import org.opensearch.test.OpenSearchTestCase;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -91,9 +86,11 @@ protected InputStream openSlice(int slice) throws IOException {
assertThat(random.nextInt(Byte.MAX_VALUE), equalTo(input.read()));
break;
default:
byte[] expectedBytes = randomBytes(random);
byte[] actualBytes = input.readNBytes(expectedBytes.length);
assertArrayEquals(expectedBytes, actualBytes);
byte[] b = randomBytes(random);
byte[] buffer = new byte[b.length];
int read = readFully(input, buffer);
assertThat(b.length, equalTo(read));
assertArrayEquals(b, buffer);
break;
}
}
Expand All @@ -110,45 +107,19 @@ protected InputStream openSlice(int slice) throws IOException {

}

public void testReadZeroLength() throws IOException {
try (InputStream input = createSingleByteStream()) {
final byte[] buffer = new byte[100];
final int read = input.read(buffer, 0, 0);
MatcherAssert.assertThat(read, equalTo(0));
}
}

public void testInvalidOffsetAndLength() throws IOException {
try (InputStream input = createSingleByteStream()) {
final byte[] buffer = new byte[100];
expectThrows(NullPointerException.class, () -> input.read(null, 0, 10));
expectThrows(IndexOutOfBoundsException.class, () -> input.read(buffer, -1, 10));
expectThrows(IndexOutOfBoundsException.class, () -> input.read(buffer, 0, -1));
expectThrows(IndexOutOfBoundsException.class, () -> input.read(buffer, 0, buffer.length + 1));
}
}

public void testReadAllBytes() throws IOException {
final byte[] expectedResults = randomByteArrayOfLength(50_000);
final int numSlices = 200;
final int slizeSize = expectedResults.length / numSlices;

final List<byte[]> arraySlices = new ArrayList<>(numSlices);
for (int i = 0; i < numSlices; i++) {
final int offset = slizeSize * i;
arraySlices.add(Arrays.copyOfRange(expectedResults, offset, offset + slizeSize));
}
// Create a SlicedInputStream that will return the expected result in 2 slices
final byte[] actualResults;
try (InputStream is = new SlicedInputStream(numSlices) {
@Override
protected InputStream openSlice(int slice) {
return new ByteArrayInputStream(arraySlices.get(slice));
private int readFully(InputStream stream, byte[] buffer) throws IOException {
for (int i = 0; i < buffer.length;) {
int read = stream.read(buffer, i, buffer.length - i);
if (read == -1) {
if (i == 0) {
return -1;
} else {
return i;
}
}
}) {
actualResults = is.readAllBytes();
i += read;
}
assertArrayEquals(expectedResults, actualResults);
return buffer.length;
}

private byte[] randomBytes(Random random) {
Expand All @@ -158,15 +129,6 @@ private byte[] randomBytes(Random random) {
return data;
}

private static InputStream createSingleByteStream() {
return new SlicedInputStream(1) {
@Override
protected InputStream openSlice(int slice) {
return new ByteArrayInputStream(new byte[] { 1 });
}
};
}

private static final class CheckClosedInputStream extends FilterInputStream {

public boolean closed = false;
Expand Down

0 comments on commit b7814c9

Please sign in to comment.