Skip to content

Commit edb20f6

Browse files
committed
linux, cmdline(), fix for #1179, comment 552984549: sometimes string ends with null byte but args are separated by spaces
1 parent d739cbb commit edb20f6

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

psutil/_pslinux.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,14 @@ def cmdline(self):
16491649
sep = '\x00' if data.endswith('\x00') else ' '
16501650
if data.endswith(sep):
16511651
data = data[:-1]
1652-
return data.split(sep)
1652+
cmdline = data.split(sep)
1653+
# Sometimes last char is a null byte '\0' but the args are
1654+
# separated by spaces, see:
1655+
# https://github.com/giampaolo/psutil/
1656+
# issues/1179#issuecomment-552984549
1657+
if sep == '\x00' and len(cmdline) == 1 and ' ' in data:
1658+
cmdline = data.split(' ')
1659+
return cmdline
16531660

16541661
@wrap_exceptions
16551662
def environ(self):

psutil/tests/test_linux.py

+8
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,14 @@ def test_cmdline_spaces_mocked(self):
18371837
self.assertEqual(p.cmdline(), ['foo', 'bar', ''])
18381838
assert m.called
18391839

1840+
def test_cmdline_mixed_separators(self):
1841+
p = psutil.Process()
1842+
fake_file = io.StringIO(u('foo\x20bar\x00'))
1843+
with mock.patch('psutil._common.open',
1844+
return_value=fake_file, create=True) as m:
1845+
self.assertEqual(p.cmdline(), ['foo', 'bar'])
1846+
assert m.called
1847+
18401848
def test_readlink_path_deleted_mocked(self):
18411849
with mock.patch('psutil._pslinux.os.readlink',
18421850
return_value='/home/foo (deleted)'):

0 commit comments

Comments
 (0)