[Java] Fixes an error allocating large direct byte buffers during OnnxTensor creation (#5619)

* Fixing an error with allocating large direct byte buffers during tensor creation.

* Removing the redundant overflow check.
This commit is contained in:
Adam Pocock 2020-11-05 18:02:41 -05:00 committed by GitHub
parent 28197b1460
commit d1d82065b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -704,15 +704,19 @@ public class OnnxTensor implements OnnxValue {
private static OnnxTensor createTensor(
OnnxJavaType type, OrtAllocator allocator, Buffer data, long[] shape) throws OrtException {
int bufferPos;
int bufferSize = data.remaining() * type.size;
if ((bufferSize < 0) || (bufferSize > ((Integer.MAX_VALUE / type.size) - 10))) {
// overflowed as we can't make a direct byte buffer of that size
long bufferSizeLong = data.remaining() * (long) type.size;
if (bufferSizeLong > (Integer.MAX_VALUE - (8 * type.size))) {
// The maximum direct byte buffer size is a little below Integer.MAX_VALUE depending
// on the JVM, so we check for something 8 elements below the maximum size which
// should be allocatable (assuming there is enough memory) on all 64-bit JVMs.
throw new IllegalStateException(
"Cannot allocate a direct buffer of the requested size and type, size "
+ data.remaining()
+ ", type = "
+ type);
}
// Now we know we're in range
int bufferSize = data.remaining() * type.size;
Buffer tmp;
if (data.isDirect()) {
tmp = data;