8000 BUG: protect stolen ref by NewFromDescr in array_empty · numpy/numpy@4282940 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4282940

Browse files
committed
BUG: protect stolen ref by NewFromDescr in array_empty
fixes #8179
1 parent 6da5f00 commit 4282940

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

numpy/core/src/multiarray/ctors.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2895,20 +2895,27 @@ PyArray_Empty(int nd, 8000 npy_intp *dims, PyArray_Descr *type, int is_f_order)
28952895
PyArrayObject *ret;
28962896

28972897
if (!type) type = PyArray_DescrFromType(NPY_DEFAULT_TYPE);
2898+
2899+
/*
2900+
* PyArray_NewFromDescr steals a ref,
2901+
* but we need to look at type later.
2902+
* */
2903+
Py_INCREF(type);
2904+
28982905
ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type,
28992906
type, nd, dims,
29002907
NULL, NULL,
29012908
is_f_order, NULL);
2902-
if (ret == NULL) {
2903-
return NULL;
2904-
}
2905-
if (PyDataType_REFCHK(type)) {
2909+
if (ret != NULL && PyDataType_REFCHK(type)) {
29062910
PyArray_FillObjectArray(ret, Py_None);
29072911
if (PyErr_Occurred()) {
29082912
Py_DECREF(ret);
2913+
Py_DECREF(type);
29092914
return NULL;
29102915
}
29112916
}
2917+
2918+
Py_DECREF(type);
29122919
return (PyObject *)ret;
29132920
}
29142921

0 commit comments

Comments
 (0)
0