From b06998b774ce0fcccb4de56ac696ff1c7f81eb3a Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Mon, 6 Feb 2023 22:15:54 +0100 Subject: [PATCH] [chip-tool] Returns an error if an unknown argument is passed to the json argument decoder (#24875) --- .../chip-tool/commands/common/Commands.cpp | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/examples/chip-tool/commands/common/Commands.cpp b/examples/chip-tool/commands/common/Commands.cpp index 749cbdcc739250..ef4386725ec2f2 100644 --- a/examples/chip-tool/commands/common/Commands.cpp +++ b/examples/chip-tool/commands/common/Commands.cpp @@ -41,21 +41,27 @@ constexpr const char * kJsonCommandKey = "command"; constexpr const char * kJsonCommandSpecifierKey = "command_specifier"; constexpr const char * kJsonArgumentsKey = "arguments"; -std::vector GetArgumentsFromJson(Command * command, Json::Value & value, bool optional) +bool GetArgumentsFromJson(Command * command, Json::Value & value, bool optional, std::vector & outArgs) { + auto memberNames = value.getMemberNames(); + std::vector args; for (size_t i = 0; i < command->GetArgumentsCount(); i++) { - auto argName = command->GetArgumentName(i); - for (auto const & memberName : value.getMemberNames()) + auto argName = command->GetArgumentName(i); + auto memberNamesIterator = memberNames.begin(); + while (memberNamesIterator != memberNames.end()) { + auto memberName = *memberNamesIterator; if (strcasecmp(argName, memberName.c_str()) != 0) { + memberNamesIterator++; continue; } if (command->GetArgumentIsOptional(i) != optional) { + memberNamesIterator = memberNames.erase(memberNamesIterator); continue; } @@ -66,10 +72,20 @@ std::vector GetArgumentsFromJson(Command * command, Json::Value & v auto argValue = value[memberName].asString(); args.push_back(std::move(argValue)); + memberNamesIterator = memberNames.erase(memberNamesIterator); break; } } - return args; + + if (memberNames.size()) + { + auto memberName = memberNames.front(); + ChipLogError(chipTool, "The argument \"\%s\" is not supported.", memberName.c_str()); + return false; + } + + outArgs = args; + return true; }; // Check for arguments with a starting '"' but no ending '"': those @@ -542,8 +558,10 @@ bool Commands::DecodeArgumentsFromBase64EncodedJson(const char * json, std::vect VerifyOrReturnValue(parsedArguments, false, ChipLogError(chipTool, "Error while parsing json.")); VerifyOrReturnValue(jsonArguments.isObject(), false, ChipLogError(chipTool, "Unexpected json type, expects and object.")); - auto mandatoryArguments = GetArgumentsFromJson(command, jsonArguments, false /* addOptional */); - auto optionalArguments = GetArgumentsFromJson(command, jsonArguments, true /* addOptional */); + std::vector mandatoryArguments; + std::vector optionalArguments; + VerifyOrReturnValue(GetArgumentsFromJson(command, jsonArguments, false /* addOptional */, mandatoryArguments), false); + VerifyOrReturnValue(GetArgumentsFromJson(command, jsonArguments, true /* addOptional */, optionalArguments), false); args.push_back(std::move(clusterName)); args.push_back(std::move(commandName));