summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2019-11-22 00:19:43 +0800
committerGiampaolo Rodola <g.rodola@gmail.com>2019-11-22 00:19:43 +0800
commitedb20f664f28653dcdd24f0bf0191984738dca6e (patch)
treee9c8f563f58ee28070113b1097cb63f1fbe3a05e
parentd739cbb1a5b207212d467b219dfc25b017911530 (diff)
downloadpsutil-edb20f664f28653dcdd24f0bf0191984738dca6e.tar.gz
linux, cmdline(), fix for #1179, comment 552984549: sometimes string ends with null byte but args are separated by spaces
-rw-r--r--psutil/_pslinux.py9
-rwxr-xr-xpsutil/tests/test_linux.py8
2 files changed, 16 insertions, 1 deletions
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index d29ccc85..80fbf8bf 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -1649,7 +1649,14 @@ class Process(object):
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):
diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py
index 09fed4e4..ccde735d 100755
--- a/psutil/tests/test_linux.py
+++ b/psutil/tests/test_linux.py
@@ -1837,6 +1837,14 @@ class TestProcess(unittest.TestCase):
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)'):