mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-26 19:52:38 +00:00
[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:
parent
28197b1460
commit
d1d82065b9
1 changed files with 7 additions and 3 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue