Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: work round int overflow in size calculation #325

Merged
merged 1 commit into from Jul 9, 2015

Conversation

matthew-brett
Copy link
Member

Omar pointed out the integer overflow in calculating the image size of an
image of size > 4GB, on a 32-bit platform.

@@ -504,7 +507,8 @@ def array_from_file(shape, in_dtype, infile, offset=0, order='F', mmap=True):
pass
if len(shape) == 0:
return np.array([])
n_bytes = int(np.prod(shape) * in_dtype.itemsize)
# Use reduce and mul to work round integer overflow on 32-bit platforms
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/round/around/g

@@ -504,7 +507,8 @@ def array_from_file(shape, in_dtype, infile, offset=0, order='F', mmap=True):
pass
if len(shape) == 0:
return np.array([])
n_bytes = int(np.prod(shape) * in_dtype.itemsize)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also specify the dtype in np.prod, rather than introduce operator.mul:

n_bytes = int(np.prod(shape, dtype=np.int64) * in_dtype.itemsize)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The advantage of using operator.mul is that is uses Python integers internally, and so completely prevents overflow for any possible value of the shape. In fact the test tests overflow for values above the int64 range, although of course you cannot make an array that large.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Might change the comment to reflect that it's not just avoiding int32 overflows, but that's really nit-picky.

Omar pointed out the integer overflow in calculating the image size of
an image of size > 4GB, on a 32-bit platform.
@matthew-brett
Copy link
Member Author

OK to merge?

@effigies
Copy link
Member

effigies commented Jul 9, 2015

Not sure who are valid signers-off, but it looks reasonable to me.

larsoner added a commit that referenced this pull request Jul 9, 2015
BUG: work round int overflow in size calculation
@larsoner larsoner merged commit c0e4ea8 into nipy:master Jul 9, 2015
@larsoner
Copy link
Contributor

larsoner commented Jul 9, 2015

Thanks @matthew-brett for the fix

@effigies effigies mentioned this pull request Nov 6, 2015
grlee77 pushed a commit to grlee77/nibabel that referenced this pull request Mar 15, 2016
BUG: work round int overflow in size calculation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants