Skip to content

Commit

Permalink
fix #926: [OSX] Process.environ() on Python 3 can crash interpreter i…
Browse files Browse the repository at this point in the history
…f process environ has an invalid unicode string.
  • Loading branch information
giampaolo committed Oct 18, 2016
1 parent d18ebfa commit 68426fc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
- #923: [OSX] free memory is wrong (does not match vm_stat command).
- #924: [OSX] Process.exe() for PID 0 erroneously raise ZombieProcess.
- #925: [OSX/BSD/SUNOS] ZombieProcess may be erroneously raised for PID 0.
- #926: [OSX] Process.environ() on Python 3 can crash interpreter if process
cwd is an invalid unicode string.


4.3.1 - 2016-09-01
Expand Down
11 changes: 8 additions & 3 deletions psutil/arch/osx/process_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ psutil_get_environ(long pid) {
env_start = arg_ptr;

procenv = calloc(1, arg_end - arg_ptr);

if (procenv == NULL) {
PyErr_NoMemory();
goto error;
Expand All @@ -296,13 +295,19 @@ psutil_get_environ(long pid) {
}

#if PY_MAJOR_VERSION >= 3
py_ret = PyUnicode_FromStringAndSize(procenv, arg_ptr - env_start + 1);
py_ret = PyUnicode_DecodeFSDefaultAndSize(
procenv, arg_ptr - env_start + 1);
#else
py_ret = PyString_FromStringAndSize(procenv, arg_ptr - env_start + 1);
#endif

if (!py_ret)
if (!py_ret) {
// XXX: don't want to free() this as per:
// https://github.com/giampaolo/psutil/issues/926
// It sucks but not sure what else to do.
procargs = NULL;
goto error;
}

free(procargs);
free(procenv);
Expand Down
1 change: 0 additions & 1 deletion psutil/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -1913,7 +1913,6 @@ def test_proc_cmdline(self):
def test_proc_cwd(self):
with chdir(self.udir):
p = psutil.Process()
print(repr(p.cwd()))
self.assertIsInstance(p.cwd(), str)
self.assertEqual(p.cwd(), self.decode_path(self.udir))

Expand Down

0 comments on commit 68426fc

Please sign in to comment.