diff --git a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp index 10459305562b48..dbb3a6fbc1c5f0 100644 --- a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp +++ b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp @@ -18,8 +18,10 @@ #include "InteractiveCommands.h" +#include #include #include +#include char kInteractiveModeName[] = ""; constexpr const char * kInteractiveModePrompt = ">>> "; @@ -102,9 +104,10 @@ bool InteractiveStartCommand::ParseCommand(char * command) char * args[kInteractiveModeArgumentsMaxLength]; args[0] = kInteractiveModeName; int argsCount = 1; + std::string arg; - char * token = strtok(command, " "); - while (token != nullptr) + std::stringstream ss(command); + while (ss >> std::quoted(arg)) { if (argsCount == kInteractiveModeArgumentsMaxLength) { @@ -114,8 +117,9 @@ bool InteractiveStartCommand::ParseCommand(char * command) return true; } - args[argsCount++] = token; - token = strtok(nullptr, " "); + char * carg = new char[arg.size() + 1]; + strcpy(carg, arg.c_str()); + args[argsCount++] = carg; } ClearLine(); @@ -123,5 +127,9 @@ bool InteractiveStartCommand::ParseCommand(char * command) mHandler->RunInteractive(argsCount, args); gIsCommandRunning = false; + // Do not delete arg[0] + while (--argsCount) + delete[] args[argsCount]; + return true; }