Skip to content

Commit a6bd532

Browse files
committed
BUG: work round int overflow in size calculation
Omar pointed out the integer overflow in calculating the image size of an image of size > 4GB, on a 32-bit platform.
1 parent ad95b4e commit a6bd532

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

nibabel/tests/test_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,3 +1227,16 @@ def assert_rt(data,
12271227
slope = slope,
12281228
post_clips = post_clips,
12291229
nan_fill = nan_fill)
1230+
1231+
1232+
def test_array_from_file_overflow():
1233+
# Test for int overflow in size calculation in array_from_file
1234+
shape = (1500,) * 6
1235+
try:
1236+
array_from_file(shape, np.int8, BytesIO())
1237+
except IOError as err:
1238+
pass
1239+
assert_equal(err.message,
1240+
'Expected {0} bytes, got {1} bytes from {2}\n'
1241+
' - could the file be damaged?'.format(
1242+
11390625000000000000, 0, 'object'))

nibabel/volumeutils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import gzip
1515
import bz2
1616
from os.path import exists, splitext
17+
from operator import mul
18+
19+
from .externals.six.moves import reduce
1720

1821
import numpy as np
1922

@@ -504,7 +507,8 @@ def array_from_file(shape, in_dtype, infile, offset=0, order='F', mmap=True):
504507
pass
505508
if len(shape) == 0:
506509
return np.array([])
507-
n_bytes = int(np.prod(shape) * in_dtype.itemsize)
510+
# Use reduce and mul to work round integer overflow on 32-bit platforms
511+
n_bytes = reduce(mul, shape) * in_dtype.itemsize
508512
if n_bytes == 0:
509513
return np.array([])
510514
# Read data from file

0 commit comments

Comments
 (0)