Skip to content

Commit

Permalink
Fixed bug when cmdline contains empty args
Browse files Browse the repository at this point in the history
When a process cmdline contains multiple null bytes in a row, psutil
splits it such that you have some empty arguments. This meant that
i3-resurrect would assume it was a multi-argument command, and would not
split cmdline[0]. I have fixed this by removing all empty arguments from
the cmdline at the beginning of get_window_command().
  • Loading branch information
JonnyHaystack committed Nov 19, 2019
1 parent 48f4ca9 commit f7ac7c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
13 changes: 8 additions & 5 deletions i3_resurrect/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,15 @@ def get_window_command(window_properties, cmdline, exe):
"""
window_command_mappings = config.get('window_command_mappings', [])

# Remove empty args from cmdline.
cmdline = [arg for arg in cmdline if arg != '']

# If cmdline has only one argument which is not a known executable path,
# try to split it. This means we can cover cases where the process
# overwrote its own cmdline, with the tradeoff that legitimate single
# argument cmdlines with a relative executable path containing spaces will
# be broken.
if len(cmdline) == 1 and shutil.which(cmdline[0]) is None:
# If cmdline has only one argument which is not a known executable
# path, try to split it. This means we can cover cases where the
# process overwrote its own cmdline, with the tradeoff that legitimate
# single argument cmdlines with a relative executable path containing
# spaces will be broken.
cmdline = shlex.split(cmdline[0])
# Use the absolute executable path in case a relative path was used.
if exe is not None:
Expand Down
15 changes: 14 additions & 1 deletion tests/test_programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,17 @@ def test_get_window_command(monkeypatch):
program8,
['/opt/Pulse SMS/pulse-sms'],
'/opt/Pulse SMS/pulse-sms',
)
) == ['/opt/Pulse SMS/pulse-sms', 'SMS/pulse-sms']

# Test cmdline with empty args is processed correctly.
assert programs.get_window_command(
program5,
['/opt/google/chrome/chrome --profile-directory=Default '
'--app=http://instacalc.com --user-data-dir=.config', '', '', '', ''],
'/opt/google/chrome/chrome',
) == [
'/opt/google/chrome/chrome',
'--profile-directory=Default',
'--app=http://instacalc.com',
'--user-data-dir=.config',
]

0 comments on commit f7ac7c0

Please sign in to comment.