From 0f53ae9a87c059ac7d8d7f76adc95d67bed4d096 Mon Sep 17 00:00:00 2001 From: Carl Suster Date: Sat, 30 Mar 2019 19:06:01 +1100 Subject: [PATCH] bpd: error instead of crashing on extra argument If an MPC client is expecting a command to take an argument that bpd isn't expecting (e.g. because of a difference in protocol versions) then bpd currently crashes completely. Instead, do what the real MPD does and return an error message over the protocol. --- beetsplug/bpd/__init__.py | 10 +++++++++- test/test_player.py | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/beetsplug/bpd/__init__.py b/beetsplug/bpd/__init__.py index 0d16e22ca..4f04d01fc 100644 --- a/beetsplug/bpd/__init__.py +++ b/beetsplug/bpd/__init__.py @@ -674,7 +674,8 @@ class Command(object): # Attempt to get correct command function. func_name = 'cmd_' + self.name if not hasattr(conn.server, func_name): - raise BPDError(ERROR_UNKNOWN, u'unknown command', self.name) + raise BPDError(ERROR_UNKNOWN, + u'unknown command "{}"'.format(self.name)) func = getattr(conn.server, func_name) # Ensure we have permission for this command. @@ -690,6 +691,13 @@ class Command(object): for data in results: yield conn.send(data) + except TypeError: + # The client provided too many arguments. + raise BPDError(ERROR_ARG, + u'wrong number of arguments for "{}"' + .format(self.name), + self.name) + except BPDError as e: # An exposed error. Set the command name and then let # the Connection handle it. diff --git a/test/test_player.py b/test/test_player.py index d7f65ff3f..d2ea496c4 100644 --- a/test/test_player.py +++ b/test/test_player.py @@ -347,6 +347,11 @@ class BPDTest(BPDTestHelper): response = client.send_command('notacommand') self._assert_failed(response, bpd.ERROR_UNKNOWN) + def test_unexpected_argument(self): + with self.run_bpd() as client: + response = client.send_command('clearerror', 'extra argument') + self._assert_failed(response, bpd.ERROR_ARG) + class BPDQueryTest(BPDTestHelper): test_implements_query = implements({