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

Prevent potential data loss in numpy's dtype::get_itemsize(). #463

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
2 changes: 1 addition & 1 deletion include/boost/python/numpy/dtype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class BOOST_NUMPY_DECL dtype : public object {
template <typename T> static dtype get_builtin();

/// @brief Return the size of the data type in bytes.
int get_itemsize() const;
boost::python::ssize_t get_itemsize() const;

/**
* @brief Compare two dtypes for equivalence.
Expand Down
2 changes: 1 addition & 1 deletion src/numpy/dtype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ python::detail::new_reference dtype::convert(object const & arg, bool align)
return python::detail::new_reference(reinterpret_cast<PyObject*>(obj));
}

int dtype::get_itemsize() const {
boost::python::ssize_t dtype::get_itemsize() const {
#if NPY_ABI_VERSION < 0x02000000
return reinterpret_cast<PyArray_Descr*>(ptr())->elsize;
#else
Expand Down
37 changes: 24 additions & 13 deletions src/numpy/ndarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ int bitflag_to_numpy(ndarray::bitflag f)

bool is_c_contiguous(std::vector<Py_intptr_t> const & shape,
std::vector<Py_intptr_t> const & strides,
int itemsize)
{
std::vector<Py_intptr_t>::const_reverse_iterator j = strides.rbegin();
int total = itemsize;
for (std::vector<Py_intptr_t>::const_reverse_iterator i = shape.rbegin(); i != shape.rend(); ++i, ++j)
boost::python::ssize_t const itemsize)
{
// An itemsize less than 0 is not useful - default to non-contiguity.
if (0 > itemsize) return false;
// Check the strides (stride[n]) match the accumulated shapes as per C-style,
// i.e. starting from rightmost C-index (itemsize * prod_{i in [n, N)} shape[i]).
std::vector<Py_intptr_t>::const_reverse_iterator j = strides.crbegin();
boost::python::ssize_t total = itemsize;
for (std::vector<Py_intptr_t>::const_reverse_iterator i = shape.crbegin(); i != shape.crend(); ++i, ++j)
{
if (total != *j) return false;
total *= (*i);
Expand All @@ -55,11 +59,15 @@ bool is_c_contiguous(std::vector<Py_intptr_t> const & shape,

bool is_f_contiguous(std::vector<Py_intptr_t> const & shape,
std::vector<Py_intptr_t> const & strides,
int itemsize)
{
std::vector<Py_intptr_t>::const_iterator j = strides.begin();
int total = itemsize;
for (std::vector<Py_intptr_t>::const_iterator i = shape.begin(); i != shape.end(); ++i, ++j)
boost::python::ssize_t const itemsize)
{
// An itemsize less than 0 is not useful - default to non-contiguity.
if (0 > itemsize) return false;
// Check the strides (stride[n]) match the accumulated shapes as per Fortran-style,
// i.e. starting from leftmost C-index (itemsize * prod_{i in [0, n]} shape[i]).
std::vector<Py_intptr_t>::const_iterator j = strides.cbegin();
boost::python::ssize_t total = itemsize;
for (std::vector<Py_intptr_t>::const_iterator i = shape.cbegin(); i != shape.cend(); ++i, ++j)
{
if (total != *j) return false;
total *= (*i);
Expand All @@ -68,9 +76,12 @@ bool is_f_contiguous(std::vector<Py_intptr_t> const & shape,
}

bool is_aligned(std::vector<Py_intptr_t> const & strides,
int itemsize)
boost::python::ssize_t const itemsize)
{
for (std::vector<Py_intptr_t>::const_iterator i = strides.begin(); i != strides.end(); ++i)
// An itemsize less than 0 is not useful - default to non-aligned.
if (0 > itemsize) return false;
// Check all strides to be aligned to itemsize.
for (std::vector<Py_intptr_t>::const_iterator i = strides.cbegin(); i != strides.cend(); ++i)
{
if (*i % itemsize) return false;
}
Expand Down Expand Up @@ -117,7 +128,7 @@ ndarray from_data_impl(void * data,
PyErr_SetString(PyExc_ValueError, "Length of shape and strides arrays do not match.");
python::throw_error_already_set();
}
int itemsize = dt.get_itemsize();
boost::python::ssize_t const itemsize = dt.get_itemsize();
int flags = 0;
if (writeable) flags |= NPY_ARRAY_WRITEABLE;
if (is_c_contiguous(shape, strides, itemsize)) flags |= NPY_ARRAY_C_CONTIGUOUS;
Expand Down