Skip to content

Commit 47dd2f0

Browse files
committed
BUG: fix memory leaks in init_iter_all when called on non C-contiguous arrays
1 parent 47ae786 commit 47dd2f0

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

bottleneck/src/iterators.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ init_iter_one(iter *it, PyArrayObject *a, int axis)
7272
* calling Py_DECREF(it.a_ravel) after you are done with the iterator.
7373
* See nanargmin for an example.
7474
*/
75-
static inline void
75+
static inline int
7676
init_iter_all(iter *it, PyArrayObject *a, int ravel, int anyorder)
7777
{
7878
int i, j = 0;
@@ -129,22 +129,30 @@ init_iter_all(iter *it, PyArrayObject *a, int ravel, int anyorder)
129129
}
130130
} else {
131131
it->ndim_m2 = -1;
132+
PyArrayObject *tmp = NULL;
132133
if (anyorder) {
133-
a = (PyArrayObject *)PyArray_Ravel(a, NPY_ANYORDER);
134+
tmp = (PyArrayObject *)PyArray_Ravel(a, NPY_ANYORDER);
134135
} else {
135-
a = (PyArrayObject *)PyArray_Ravel(a, NPY_CORDER);
136+
tmp = (PyArrayObject *)PyArray_Ravel(a, NPY_CORDER);
136137
}
138+
Py_DECREF(a);
139+
if (tmp == NULL) return -1;
140+
a = tmp;
137141
it->a_ravel = a;
138142
it->length = PyArray_DIM(a, 0);
139143
it->astride = PyArray_STRIDE(a, 0);
140144
}
141145
} else if (ravel) {
142146
it->ndim_m2 = -1;
147+
PyArrayObject *tmp = NULL;
143148
if (anyorder) {
144-
a = (PyArrayObject *)PyArray_Ravel(a, NPY_ANYORDER);
149+
tmp = (PyArrayObject *)PyArray_Ravel(a, NPY_ANYORDER);
145150
} else {
146-
a = (PyArrayObject *)PyArray_Ravel(a, NPY_CORDER);
151+
tmp = (PyArrayObject *)PyArray_Ravel(a, NPY_CORDER);
147152
}
153+
Py_DECREF(a);
154+
if (tmp == NULL) return -1;
155+
a = tmp;
148156
it->a_ravel = a;
149157
it->length = PyArray_DIM(a, 0);
150158
it->astride = PyArray_STRIDE(a, 0);
@@ -171,6 +179,7 @@ init_iter_all(iter *it, PyArrayObject *a, int ravel, int anyorder)
171179

172180
it->stride = it->astride / item_size;
173181
it->pa = PyArray_BYTES(a);
182+
return 0;
174183
}
175184

176185
#define NEXT \

0 commit comments

Comments
 (0)