-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix possible double close and use of unopened socket #825
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
|
@@ -538,17 +538,17 @@ 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; | ||
close(sock); | ||
Py_DECREF(py_is_up); | ||
return py_retlist; | ||
|
||
error: | ||
Py_XDECREF(py_is_up); | ||
if (sock != 0) | ||
if (sock != -1) | ||
close(sock); | ||
Py_XDECREF(py_is_up); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kinda don't like having two separate goto handlers. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, once a socket is closed - the file descriptor is available for use elsewhere. And thus a double close could end up closing that - which may cause a problem elsewhere. If you don't like multiple goto handlers, you might prefer moving the close(sock) on line 541 down to 545 before the return. That way there is a close in every path in the function - and no double close. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that. Can you update the PR also including changes for the HISTORY.rst file? |
||
PyErr_SetFromErrno(PyExc_OSError); | ||
return NULL; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please specify the platform (
[Linux]
) and the function which was affected (the high level, end-user psutil function, not the C function)