Skip to content

Commit

Permalink
fix #2116, macOS, net_connections() crashing
Browse files Browse the repository at this point in the history
It turns out that proc_pidinfo() crashes for PID 0

Signed-off-by: Giampaolo Rodola <[email protected]>
  • Loading branch information
giampaolo committed Sep 19, 2022
1 parent b88f4d8 commit f4784d0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ XXXX-XX-XX

**Bug fixes**

- 2116_, [macOS]: `psutil.net_connections`_ fails with RuntimeError.
- 2135_, [macOS]: `Process.environ()`_ may contain garbage data. Fix
out-of-bounds read around ``sysctl_procargs``. (patch by Bernhard Urban-Forster)
- 2138_, [Linux], **[critical]**: can't compile psutil on Android due to
Expand Down
8 changes: 8 additions & 0 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,10 @@ psutil_proc_open_files(PyObject *self, PyObject *args) {
if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid))
goto error;

// see: https://github.com/giampaolo/psutil/issues/2116
if (pid == 0)
return py_retlist;

fds_pointer = psutil_proc_list_fds(pid, &num_fds);
if (fds_pointer == NULL)
goto error;
Expand Down Expand Up @@ -1047,6 +1051,10 @@ psutil_proc_connections(PyObject *self, PyObject *args) {
goto error;
}

// see: https://github.com/giampaolo/psutil/issues/2116
if (pid == 0)
return py_retlist;

if (!PySequence_Check(py_af_filter) || !PySequence_Check(py_type_filter)) {
PyErr_SetString(PyExc_TypeError, "arg 2 or 3 is not a sequence");
goto error;
Expand Down

0 comments on commit f4784d0

Please sign in to comment.