From 36fcc17fdcc233c1ebb96903eea2973ae67089c4 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 4 Dec 2017 20:03:46 +0100 Subject: BUG: Fix DECREF before last usage when evaluating array interface --- numpy/core/src/multiarray/ctors.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'numpy/core/src') diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 606c3e81f..bb93d4ff7 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -2207,8 +2207,10 @@ PyArray_FromInterface(PyObject *origin) /* Get type string from interface specification */ attr = PyDict_GetItemString(iface, "typestr"); + Py_INCREF(attr); /* incref so it is easier to replace */ if (attr == NULL) { Py_DECREF(iface); + Py_DECREF(attr); PyErr_SetString(PyExc_ValueError, "Missing __array_interface__ typestr"); return NULL; @@ -2217,6 +2219,7 @@ PyArray_FromInterface(PyObject *origin) /* Allow unicode type strings */ if (PyUnicode_Check(attr)) { tmp = PyUnicode_AsASCIIString(attr); + Py_DECREF(attr); attr = tmp; } #endif @@ -2227,11 +2230,6 @@ PyArray_FromInterface(PyObject *origin) } /* Get dtype from type string */ dtype = _array_typedescr_fromstr(PyString_AS_STRING(attr)); -#if defined(NPY_PY3K) - if (tmp == attr) { - Py_DECREF(tmp); - } -#endif if (dtype == NULL) { goto fail; } @@ -2251,6 +2249,7 @@ PyArray_FromInterface(PyObject *origin) dtype = new_dtype; } } + Py_DECREF(attr); /* Get shape tuple from interface specification */ attr = PyDict_GetItemString(iface, "shape"); -- cgit v1.2.1 From 441fa29888bb1a7b33261e830a37ab6811440a3b Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 4 Dec 2017 20:04:34 +0100 Subject: BUG: Only read second part of rational tuple if two values exist This is probably harmless, but valgrind notices it --- numpy/core/src/umath/test_rational.c.src | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'numpy/core/src') diff --git a/numpy/core/src/umath/test_rational.c.src b/numpy/core/src/umath/test_rational.c.src index 26c3d3799..ffc92b732 100644 --- a/numpy/core/src/umath/test_rational.c.src +++ b/numpy/core/src/umath/test_rational.c.src @@ -394,14 +394,14 @@ pyrational_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { return 0; } size = PyTuple_GET_SIZE(args); - if (size>2) { + if (size > 2) { PyErr_SetString(PyExc_TypeError, "expected rational or numerator and optional denominator"); return 0; } - x[0] = PyTuple_GET_ITEM(args,0); - x[1] = PyTuple_GET_ITEM(args,1); - if (size==1) { + + if (size == 1) { + x[0] = PyTuple_GET_ITEM(args, 0); if (PyRational_Check(x[0])) { Py_INCREF(x[0]); return x[0]; @@ -424,9 +424,11 @@ pyrational_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { return 0; } } - for (i=0;i Date: Sat, 9 Dec 2017 12:04:21 -0800 Subject: BUG: Fix null pointer dereference And tweak refcounts --- numpy/core/src/multiarray/ctors.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'numpy/core/src') diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index bb93d4ff7..22b3d419d 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -2181,7 +2181,6 @@ _is_default_descr(PyObject *descr, PyObject *typestr) { NPY_NO_EXPORT PyObject * PyArray_FromInterface(PyObject *origin) { - PyObject *tmp = NULL; PyObject *iface = NULL; PyObject *attr = NULL; PyObject *base = NULL; @@ -2207,10 +2206,8 @@ PyArray_FromInterface(PyObject *origin) /* Get type string from interface specification */ attr = PyDict_GetItemString(iface, "typestr"); - Py_INCREF(attr); /* incref so it is easier to replace */ if (attr == NULL) { Py_DECREF(iface); - Py_DECREF(attr); PyErr_SetString(PyExc_ValueError, "Missing __array_interface__ typestr"); return NULL; @@ -2218,10 +2215,15 @@ PyArray_FromInterface(PyObject *origin) #if defined(NPY_PY3K) /* Allow unicode type strings */ if (PyUnicode_Check(attr)) { - tmp = PyUnicode_AsASCIIString(attr); - Py_DECREF(attr); + PyObject *tmp = PyUnicode_AsASCIIString(attr); + if (tmp == NULL) { + goto fail; + } attr = tmp; } + else { + Py_INCREF(attr); + } #endif if (!PyBytes_Check(attr)) { PyErr_SetString(PyExc_TypeError, @@ -2249,7 +2251,10 @@ PyArray_FromInterface(PyObject *origin) dtype = new_dtype; } } - Py_DECREF(attr); + +#if defined(NPY_PY3K) + Py_DECREF(attr); /* Pairs with the unicode handling above */ +#endif /* Get shape tuple from interface specification */ attr = PyDict_GetItemString(iface, "shape"); @@ -2277,7 +2282,7 @@ PyArray_FromInterface(PyObject *origin) else { n = PyTuple_GET_SIZE(attr); for (i = 0; i < n; i++) { - tmp = PyTuple_GET_ITEM(attr, i); + PyObject *tmp = PyTuple_GET_ITEM(attr, i); dims[i] = PyArray_PyIntAsIntp(tmp); if (error_converting(dims[i])) { goto fail; @@ -2394,7 +2399,7 @@ PyArray_FromInterface(PyObject *origin) goto fail; } for (i = 0; i < n; i++) { - tmp = PyTuple_GET_ITEM(attr, i); + PyObject *tmp = PyTuple_GET_ITEM(attr, i); strides[i] = PyArray_PyIntAsIntp(tmp); if (error_converting(strides[i])) { Py_DECREF(ret); -- cgit v1.2.1