Skip to content

Commit

Permalink
TIKA-1924 - needed to add sanity check in map()
Browse files Browse the repository at this point in the history
  • Loading branch information
tballison committed Apr 26, 2016
1 parent ab7c325 commit 7a543c8
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;

Expand Down Expand Up @@ -66,6 +67,9 @@ public int read(ByteBuffer byteBuffer) throws IOException {
}

public int readAllInOnce(ByteBuffer byteBuffer) throws IOException {
if (byteBuffer.remaining() > raf.length()) {
throw new IOException("trying to readAllInOnce past end of stream");
}
byte[] buf = new byte[byteBuffer.remaining()];
int read = raf.read(buf);
byteBuffer.put(buf, 0, read);
Expand All @@ -81,6 +85,9 @@ public long position() throws IOException {
}

public void position(long nuPos) throws IOException {
if (nuPos > raf.length()) {
throw new IOException("requesting seek past end of stream");
}
raf.seek(nuPos);
}

Expand All @@ -89,12 +96,30 @@ public long transferTo(long position, long count, WritableByteChannel target) th
}

public ByteBuffer map(long startPosition, long size) throws IOException {
if (startPosition < 0 || size < 0) {
throw new IOException("startPosition and size must both be >= 0");
}
//make sure that start+size aren't greater than avail size
//in raf.
BigInteger end = BigInteger.valueOf(startPosition);
end.add(BigInteger.valueOf(size));
if (end.compareTo(BigInteger.valueOf(raf.length())) > 0) {
throw new IOException("requesting read past end of stream");
}

raf.seek(startPosition);
byte[] payload = new byte[l2i(size)];
int payLoadSize = l2i(size);
//hack to check for potential overflow
if (Long.MAX_VALUE-payLoadSize < startPosition ||
Long.MAX_VALUE-payLoadSize > raf.length()) {
throw new IOException("requesting read past end of stream");
}
byte[] payload = new byte[payLoadSize];
raf.readFully(payload);
return ByteBuffer.wrap(payload);
}

@Override
public void close() throws IOException {
raf.close();
}
Expand Down
Loading

0 comments on commit 7a543c8

Please sign in to comment.