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: Fixes for Undefined Behavior Sanitizer (UBSan) errors. #14043

Merged
merged 2 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions numpy/core/src/multiarray/arrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1163,8 +1163,10 @@ _void_compare(PyArrayObject *self, PyArrayObject *other, int cmp_op)

newdims.ptr = dimensions;
newdims.len = result_ndim+1;
memcpy(dimensions, PyArray_DIMS((PyArrayObject *)temp),
sizeof(npy_intp)*result_ndim);
if (result_ndim) {
memcpy(dimensions, PyArray_DIMS((PyArrayObject *)temp),
sizeof(npy_intp)*result_ndim);
}
dimensions[result_ndim] = -1;
temp2 = PyArray_Newshape((PyArrayObject *)temp,
&newdims, NPY_ANYORDER);
Expand Down
12 changes: 9 additions & 3 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,9 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
goto fail;
}
fa->strides = fa->dimensions + nd;
memcpy(fa->dimensions, dims, sizeof(npy_intp)*nd);
if (nd) {
memcpy(fa->dimensions, dims, sizeof(npy_intp)*nd);
}
if (strides == NULL) { /* fill it in */
_array_fill_strides(fa->strides, dims, nd, descr->elsize,
flags, &(fa->flags));
Expand All @@ -1041,7 +1043,9 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
* we allow strides even when we create
* the memory, but be careful with this...
*/
memcpy(fa->strides, strides, sizeof(npy_intp)*nd);
if (nd) {
memcpy(fa->strides, strides, sizeof(npy_intp)*nd);
}
}
}
else {
Expand Down Expand Up @@ -2537,7 +2541,9 @@ PyArray_FromInterface(PyObject *origin)
goto fail;
}
}
memcpy(PyArray_STRIDES(ret), strides, n*sizeof(npy_intp));
if (n) {
memcpy(PyArray_STRIDES(ret), strides, n*sizeof(npy_intp));
}
}
PyArray_UpdateFlags(ret, NPY_ARRAY_UPDATE_ALL);
Py_DECREF(iface);
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/src/multiarray/dragon4.c
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ BigInt_Pow2(BigInt *result, npy_uint32 exponent)
result->length = blockIdx + 1;

bitIdx = (exponent % 32);
result->blocks[blockIdx] |= (1 << bitIdx);
result->blocks[blockIdx] |= ((npy_uint32)1 << bitIdx);
}

/*
Expand Down
16 changes: 11 additions & 5 deletions numpy/core/src/multiarray/getset.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ array_shape_set(PyArrayObject *self, PyObject *val)
return -1;
}
((PyArrayObject_fields *)self)->strides = PyArray_DIMS(self) + nd;
memcpy(PyArray_DIMS(self), PyArray_DIMS(ret), nd*sizeof(npy_intp));
memcpy(PyArray_STRIDES(self), PyArray_STRIDES(ret), nd*sizeof(npy_intp));
if (nd) {
memcpy(PyArray_DIMS(self), PyArray_DIMS(ret), nd*sizeof(npy_intp));
memcpy(PyArray_STRIDES(self), PyArray_STRIDES(ret), nd*sizeof(npy_intp));
}
}
else {
((PyArrayObject_fields *)self)->dimensions = NULL;
Expand Down Expand Up @@ -172,7 +174,9 @@ array_strides_set(PyArrayObject *self, PyObject *obj)
"compatible with available memory");
goto fail;
}
memcpy(PyArray_STRIDES(self), newstrides.ptr, sizeof(npy_intp)*newstrides.len);
if (newstrides.len) {
memcpy(PyArray_STRIDES(self), newstrides.ptr, sizeof(npy_intp)*newstrides.len);
}
PyArray_UpdateFlags(self, NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS |
NPY_ARRAY_ALIGNED);
npy_free_cache_dim_obj(newstrides);
Expand Down Expand Up @@ -669,8 +673,10 @@ array_struct_get(PyArrayObject *self)
return PyErr_NoMemory();
}
inter->strides = inter->shape + PyArray_NDIM(self);
memcpy(inter->shape, PyArray_DIMS(self), sizeof(npy_intp)*PyArray_NDIM(self));
memcpy(inter->strides, PyArray_STRIDES(self), sizeof(npy_intp)*PyArray_NDIM(self));
if (PyArray_NDIM(self)) {
memcpy(inter->shape, PyArray_DIMS(self), sizeof(npy_intp)*PyArray_NDIM(self));
memcpy(inter->strides, PyArray_STRIDES(self), sizeof(npy_intp)*PyArray_NDIM(self));
}
}
else {
inter->shape = NULL;
Expand Down
4 changes: 3 additions & 1 deletion numpy/core/src/multiarray/methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,9 @@ array_setstate(PyArrayObject *self, PyObject *args)
return PyErr_NoMemory();
}
fa->strides = PyArray_DIMS(self) + nd;
memcpy(PyArray_DIMS(self), dimensions, sizeof(npy_intp)*nd);
if (nd) {
memcpy(PyArray_DIMS(self), dimensions, sizeof(npy_intp)*nd);
}
_array_fill_strides(PyArray_STRIDES(self), dimensions, nd,
PyArray_DESCR(self)->elsize,
(is_f_order ? NPY_ARRAY_F_CONTIGUOUS :
Expand Down
4 changes: 3 additions & 1 deletion numpy/core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ PyArray_AsCArray(PyObject **op, void *ptr, npy_intp *dims, int nd,
}
*((char ****)ptr) = ptr3;
}
memcpy(dims, PyArray_DIMS(ap), nd*sizeof(npy_intp));
if (nd) {
memcpy(dims, PyArray_DIMS(ap), nd*sizeof(npy_intp));
}
*op = (PyObject *)ap;
return 0;
}
Expand Down
8 changes: 6 additions & 2 deletions numpy/core/src/umath/reduction.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ allocate_reduce_result(PyArrayObject *arr, npy_bool *axis_flags,

/* Build the new strides and shape */
stride = dtype->elsize;
memcpy(shape, arr_shape, ndim * sizeof(shape[0]));
if (ndim) {
memcpy(shape, arr_shape, ndim * sizeof(shape[0]));
}
for (idim = ndim-1; idim >= 0; --idim) {
npy_intp i_perm = strideperm[idim].perm;
if (axis_flags[i_perm]) {
Expand Down Expand Up @@ -325,7 +327,9 @@ PyArray_InitializeReduceResult(
*/
shape = PyArray_SHAPE(op_view);
nreduce_axes = 0;
memcpy(shape_orig, shape, ndim * sizeof(npy_intp));
if (ndim) {
memcpy(shape_orig, shape, ndim * sizeof(npy_intp));
}
for (idim = 0; idim < ndim; ++idim) {
if (axis_flags[idim]) {
if (shape[idim] == 0) {
Expand Down