Author: Antonio Cuni <[email protected]>
Branch: ndarray-buffer
Changeset: r68243:8d2b54062cfc
Date: 2013-11-19 15:33 +0100
http://bitbucket.org/pypy/pypy/changeset/8d2b54062cfc/
Log: check the size of the buffer
diff --git a/pypy/module/micronumpy/interp_numarray.py
b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -22,6 +22,7 @@
from rpython.rlib.rstring import StringBuilder
from pypy.module.micronumpy.arrayimpl.base import BaseArrayImplementation
from pypy.module.micronumpy.conversion_utils import order_converter,
multi_axis_converter
+from pypy.module.micronumpy import support
from pypy.module.micronumpy.constants import *
def _find_shape(space, w_size, dtype):
@@ -1085,6 +1086,10 @@
if not shape:
raise OperationError(space.w_TypeError, space.wrap(
"numpy scalars from buffers not supported yet"))
+ totalsize = support.product(shape) * dtype.get_size()
+ if totalsize+offset > buf.getlength():
+ raise OperationError(space.w_TypeError, space.wrap(
+ "buffer is too small for requested array"))
storage = rffi.cast(RAW_STORAGE_PTR, raw_ptr)
storage = rffi.ptradd(storage, offset)
return W_NDimArray.from_shape_and_storage(space, shape, storage, dtype,
diff --git a/pypy/module/micronumpy/test/test_numarray.py
b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -2109,6 +2109,16 @@
a[2] = ord('r')
assert list(buf) == ['X', 'b', '\x00', 'a', '\x00', 'r', '\x00']
+ def test_ndarray_from_buffer_out_of_bounds(self):
+ import numpypy as np
+ import array
+ buf = array.array('c', ['\x00']*2*10) # 20 bytes
+ info = raises(TypeError, "np.ndarray((11,), buffer=buf, dtype='i2')")
+ assert str(info.value).startswith('buffer is too small')
+ info = raises(TypeError, "np.ndarray((5,), buffer=buf, offset=15,
dtype='i2')")
+ assert str(info.value).startswith('buffer is too small')
+
+
class AppTestMultiDim(BaseNumpyAppTest):
def test_init(self):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit