Skip to content

Commit

Permalink
linux, cmdline(), fix for #1179, comment 552984549: sometimes string …
Browse files Browse the repository at this point in the history
…ends with null byte but args are separated by spaces
  • Loading branch information
giampaolo committed Nov 21, 2019
1 parent d739cbb commit edb20f6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,14 @@ def cmdline(self):
sep = '\x00' if data.endswith('\x00') else ' '
if data.endswith(sep):
data = data[:-1]
return data.split(sep)
cmdline = data.split(sep)
# Sometimes last char is a null byte '\0' but the args are
# separated by spaces, see:
# https://github.com/giampaolo/psutil/
# issues/1179#issuecomment-552984549
if sep == '\x00' and len(cmdline) == 1 and ' ' in data:
cmdline = data.split(' ')
return cmdline

@wrap_exceptions
def environ(self):
Expand Down
8 changes: 8 additions & 0 deletions psutil/tests/test_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1837,6 +1837,14 @@ def test_cmdline_spaces_mocked(self):
self.assertEqual(p.cmdline(), ['foo', 'bar', ''])
assert m.called

def test_cmdline_mixed_separators(self):
p = psutil.Process()
fake_file = io.StringIO(u('foo\x20bar\x00'))
with mock.patch('psutil._common.open',
return_value=fake_file, create=True) as m:
self.assertEqual(p.cmdline(), ['foo', 'bar'])
assert m.called

def test_readlink_path_deleted_mocked(self):
with mock.patch('psutil._pslinux.os.readlink',
return_value='/home/foo (deleted)'):
Expand Down

0 comments on commit edb20f6

Please sign in to comment.