From b856d857b4ba29059b440f65870c6e3f08b7e615 Mon Sep 17 00:00:00 2001 From: Shreedhar Hardikar Date: Tue, 24 May 2016 16:27:56 +0000 Subject: [PATCH 1/2] Fix possible double close and use of unopened socket --- psutil/_psutil_linux.c | 9 +++++---- psutil/arch/bsd/freebsd.c | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/psutil/_psutil_linux.c b/psutil/_psutil_linux.c index 7d2ca2a44..afc71580a 100644 --- a/psutil/_psutil_linux.c +++ b/psutil/_psutil_linux.c @@ -404,7 +404,7 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) { #else long value = PyInt_AsLong(item); #endif - if (value == -1 && PyErr_Occurred()) + if (value == -1 || PyErr_Occurred()) goto error; CPU_SET(value, &cpu_set); } @@ -541,14 +541,15 @@ psutil_net_if_stats(PyObject* self, PyObject* args) { close(sock); py_retlist = Py_BuildValue("[Oiii]", py_is_up, duplex, speed, mtu); if (!py_retlist) - goto error; + goto error_after_close; Py_DECREF(py_is_up); return py_retlist; error: - Py_XDECREF(py_is_up); - if (sock != 0) + if (sock != -1) close(sock); +error_after_close: + Py_XDECREF(py_is_up); PyErr_SetFromErrno(PyExc_OSError); return NULL; } diff --git a/psutil/arch/bsd/freebsd.c b/psutil/arch/bsd/freebsd.c index d335a7ade..f7e15f758 100644 --- a/psutil/arch/bsd/freebsd.c +++ b/psutil/arch/bsd/freebsd.c @@ -971,7 +971,7 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) { #else long value = PyInt_AsLong(item); #endif - if (value == -1 && PyErr_Occurred()) + if (value == -1 || PyErr_Occurred()) goto error; CPU_SET(value, &cpu_set); } From f6269a6321a41bf6e9490425e1549e24cce225c7 Mon Sep 17 00:00:00 2001 From: Shreedhar Hardikar Date: Tue, 14 Jun 2016 00:21:45 +0000 Subject: [PATCH 2/2] Address PR comments --- HISTORY.rst | 1 + psutil/_psutil_linux.c | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index e8a6ed9a5..b8521e0a7 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -19,6 +19,7 @@ Bug tracker at https://github.com/giampaolo/psutil/issues - #797: [Linux] net_if_stats() may raise OSError for certain NIC cards. - #813: Process.as_dict() should ignore extraneous attribute names which gets attached to the Process instance. +- #825: Fix possible double close and use of unopened socket 4.1.0 - 2016-03-12 diff --git a/psutil/_psutil_linux.c b/psutil/_psutil_linux.c index afc71580a..fb33c165e 100644 --- a/psutil/_psutil_linux.c +++ b/psutil/_psutil_linux.c @@ -538,17 +538,16 @@ psutil_net_if_stats(PyObject* self, PyObject* args) { } } - close(sock); py_retlist = Py_BuildValue("[Oiii]", py_is_up, duplex, speed, mtu); if (!py_retlist) - goto error_after_close; + goto error; + close(sock); Py_DECREF(py_is_up); return py_retlist; error: if (sock != -1) close(sock); -error_after_close: Py_XDECREF(py_is_up); PyErr_SetFromErrno(PyExc_OSError); return NULL;