From e118e958f7f9b87054679ebf6102d6923abbba81 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 11 Aug 2015 22:57:06 +0200 Subject: [PATCH] protocol/ArgParser: add overload with max_value parameter --- src/command/OtherCommands.cxx | 14 ++------------ src/command/QueueCommands.cxx | 16 ++-------------- src/protocol/ArgParser.cxx | 26 +++++++++++++++++++++----- src/protocol/ArgParser.hxx | 8 ++++++++ 4 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/command/OtherCommands.cxx b/src/command/OtherCommands.cxx index 4fc7e56f6..816764a5d 100644 --- a/src/command/OtherCommands.cxx +++ b/src/command/OtherCommands.cxx @@ -309,14 +309,9 @@ CommandResult handle_setvol(Client &client, Request args) { unsigned level; - if (!ParseCommandArg(client, level, args.front())) + if (!ParseCommandArg(client, level, args.front(), 100)) return CommandResult::ERROR; - if (level > 100) { - command_error(client, ACK_ERROR_ARG, "Invalid volume value"); - return CommandResult::ERROR; - } - if (!volume_level_change(client.partition.outputs, level)) { command_error(client, ACK_ERROR_SYSTEM, "problems setting volume"); @@ -330,14 +325,9 @@ CommandResult handle_volume(Client &client, Request args) { int relative; - if (!ParseCommandArg(client, relative, args.front())) + if (!ParseCommandArg(client, relative, args.front(), -100, 100)) return CommandResult::ERROR; - if (relative < -100 || relative > 100) { - command_error(client, ACK_ERROR_ARG, "Invalid volume value"); - return CommandResult::ERROR; - } - const int old_volume = volume_level_get(client.partition.outputs); if (old_volume < 0) { command_error(client, ACK_ERROR_SYSTEM, "No mixer"); diff --git a/src/command/QueueCommands.cxx b/src/command/QueueCommands.cxx index 634510cc8..9a64ceb25 100644 --- a/src/command/QueueCommands.cxx +++ b/src/command/QueueCommands.cxx @@ -314,15 +314,9 @@ handle_prio(Client &client, Request args) { const char *const priority_string = args.shift(); unsigned priority; - if (!ParseCommandArg(client, priority, priority_string)) + if (!ParseCommandArg(client, priority, priority_string, 0xff)) return CommandResult::ERROR; - if (priority > 0xff) { - command_error(client, ACK_ERROR_ARG, - "Priority out of range: %s", priority_string); - return CommandResult::ERROR; - } - for (const char *i : args) { RangeArg range; if (!ParseCommandArg(client, range, i)) @@ -344,15 +338,9 @@ handle_prioid(Client &client, Request args) { const char *const priority_string = args.shift(); unsigned priority; - if (!ParseCommandArg(client, priority, priority_string)) + if (!ParseCommandArg(client, priority, priority_string, 0xff)) return CommandResult::ERROR; - if (priority > 0xff) { - command_error(client, ACK_ERROR_ARG, - "Priority out of range: %s", priority_string); - return CommandResult::ERROR; - } - for (const char *i : args) { unsigned song_id; if (!ParseCommandArg(client, song_id, i)) diff --git a/src/protocol/ArgParser.cxx b/src/protocol/ArgParser.cxx index 1dbbd0cf3..580d70a20 100644 --- a/src/protocol/ArgParser.cxx +++ b/src/protocol/ArgParser.cxx @@ -39,7 +39,8 @@ check_uint32(Client &client, uint32_t *dst, const char *s) } bool -ParseCommandArg(Client &client, int &value_r, const char *s) +ParseCommandArg(Client &client, int &value_r, const char *s, + int min_value, int max_value) { char *test; long value; @@ -51,8 +52,7 @@ ParseCommandArg(Client &client, int &value_r, const char *s) return false; } - if (value < std::numeric_limits::min() || - value > std::numeric_limits::max()) { + if (value < min_value || value > max_value) { command_error(client, ACK_ERROR_ARG, "Number too large: %s", s); return false; @@ -62,6 +62,14 @@ ParseCommandArg(Client &client, int &value_r, const char *s) return true; } +bool +ParseCommandArg(Client &client, int &value_r, const char *s) +{ + return ParseCommandArg(client, value_r, s, + std::numeric_limits::min(), + std::numeric_limits::max()); +} + bool ParseCommandArg(Client &client, RangeArg &value_r, const char *s) { @@ -129,7 +137,8 @@ ParseCommandArg(Client &client, RangeArg &value_r, const char *s) } bool -ParseCommandArg(Client &client, unsigned &value_r, const char *s) +ParseCommandArg(Client &client, unsigned &value_r, const char *s, + unsigned max_value) { unsigned long value; char *endptr; @@ -141,7 +150,7 @@ ParseCommandArg(Client &client, unsigned &value_r, const char *s) return false; } - if (value > std::numeric_limits::max()) { + if (value > max_value) { command_error(client, ACK_ERROR_ARG, "Number too large: %s", s); return false; @@ -151,6 +160,13 @@ ParseCommandArg(Client &client, unsigned &value_r, const char *s) return true; } +bool +ParseCommandArg(Client &client, unsigned &value_r, const char *s) +{ + return ParseCommandArg(client, value_r, s, + std::numeric_limits::max()); +} + bool ParseCommandArg(Client &client, bool &value_r, const char *s) { diff --git a/src/protocol/ArgParser.hxx b/src/protocol/ArgParser.hxx index 221ffebe3..87f15846a 100644 --- a/src/protocol/ArgParser.hxx +++ b/src/protocol/ArgParser.hxx @@ -33,6 +33,10 @@ class SignedSongTime; bool check_uint32(Client &client, uint32_t *dst, const char *s); +bool +ParseCommandArg(Client &client, int &value_r, const char *s, + int min_value, int max_value); + bool ParseCommandArg(Client &client, int &value_r, const char *s); @@ -48,6 +52,10 @@ struct RangeArg { bool ParseCommandArg(Client &client, RangeArg &value_r, const char *s); +bool +ParseCommandArg(Client &client, unsigned &value_r, const char *s, + unsigned max_value); + bool ParseCommandArg(Client &client, unsigned &value_r, const char *s);