Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,10 @@ public long skip(long n) throws IOException {
checkStream();

// Read 'n' bytes
int skipped = 0;
long skipped = 0;
while (skipped < n) {
int len = Math.min(((int)n - skipped), skipBytes.length);
// len is between 0 and skipBytes.length, so downcast is safe
int len = (int)Math.min((n - skipped), skipBytes.length);
len = read(skipBytes, 0, len);
if (len == -1) {
eof = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -48,6 +49,26 @@ public void testRead2() throws IOException {
testRead(4);
}

@Test
public void testDecompressorStreamSkipOutOfBound() throws IOException {
ByteArrayInputStream bytesIn;
DecompressorStream decompStream;
int bufLen = 4;
bytesOut = new ByteArrayOutputStream();
bytesOut.write(ByteBuffer.allocate(bufLen).putInt(1024).array(), 0, bufLen);
buf = bytesOut.toByteArray();
bytesIn = new ByteArrayInputStream(buf);

decompStream = new DecompressorStream(bytesIn, new FakeDecompressor());
try {
// check that skipping a length longer than Integer.MAX_VALUE does not
// throw IndexOutOfBoundsException
decompStream.skip((long)Integer.MAX_VALUE + 1);
} catch (EOFException e) {
// expected exception, because the input is not that long
}
}

private void testRead(int bufLen) throws IOException {
// compress empty stream
bytesOut = new ByteArrayOutputStream();
Expand Down