summaryrefslogtreecommitdiff
path: root/psutil/_psutil_linux.c
diff options
context:
space:
mode:
authorRiccardo Schirone <ret2libc@users.noreply.github.com>2019-11-13 14:54:21 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2019-11-13 21:54:21 +0800
commit7d512c8e4442a896d56505be3e78f1156f443465 (patch)
tree387459430317a6ba3e5975697e7f2ce069197bf1 /psutil/_psutil_linux.c
parentef032158fd72790ab2be14e99c45c32d66d96291 (diff)
downloadpsutil-7d512c8e4442a896d56505be3e78f1156f443465.tar.gz
Use Py_CLEAR instead of Py_DECREF to also set the variable to NULL (#1616)
These files contain loops that convert system data into python objects and during the process they create objects and dereference their refcounts after they have been added to the resulting list. However, in case of errors during the creation of those python objects, the refcount to previously allocated objects is dropped again with Py_XDECREF, which should be a no-op in case the paramater is NULL. Even so, in most of these loops the variables pointing to the objects are never set to NULL, even after Py_DECREF is called at the end of the loop iteration. This means, after the first iteration, if an error occurs those python objects will get their refcount dropped two times, resulting in a possible double-free.
Diffstat (limited to 'psutil/_psutil_linux.c')
-rw-r--r--psutil/_psutil_linux.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/psutil/_psutil_linux.c b/psutil/_psutil_linux.c
index 717723d0..0d16eb42 100644
--- a/psutil/_psutil_linux.c
+++ b/psutil/_psutil_linux.c
@@ -241,9 +241,9 @@ psutil_disk_partitions(PyObject *self, PyObject *args) {
goto error;
if (PyList_Append(py_retlist, py_tuple))
goto error;
- Py_DECREF(py_dev);
- Py_DECREF(py_mountp);
- Py_DECREF(py_tuple);
+ Py_CLEAR(py_dev);
+ Py_CLEAR(py_mountp);
+ Py_CLEAR(py_tuple);
}
endmntent(file);
return py_retlist;
@@ -454,10 +454,10 @@ psutil_users(PyObject *self, PyObject *args) {
goto error;
if (PyList_Append(py_retlist, py_tuple))
goto error;
- Py_DECREF(py_username);
- Py_DECREF(py_tty);
- Py_DECREF(py_hostname);
- Py_DECREF(py_tuple);
+ Py_CLEAR(py_username);
+ Py_CLEAR(py_tty);
+ Py_CLEAR(py_hostname);
+ Py_CLEAR(py_tuple);
}
endutent();
return py_retlist;