From 899e00a5fba6296bf44d7b7aa2f43a576d2ebbe7 Mon Sep 17 00:00:00 2001 From: Lorow Date: Sat, 10 May 2025 21:01:39 +0200 Subject: [PATCH] Find a bug with multi-command execution, fix crash when data was missing --- .../CommandManager/CommandManager.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/components/CommandManager/CommandManager/CommandManager.cpp b/components/CommandManager/CommandManager/CommandManager.cpp index 9031580..8b28063 100644 --- a/components/CommandManager/CommandManager/CommandManager.cpp +++ b/components/CommandManager/CommandManager/CommandManager.cpp @@ -59,19 +59,31 @@ CommandResult CommandManager::executeFromJson(std::string_view json) { cJSON *parsedJson = cJSON_Parse(json.data()); if (parsedJson == nullptr) - return CommandResult::getErrorResult("Invalid JSON"); + return CommandResult::getErrorResult("Initial JSON Parse: Invalid JSON"); const cJSON *commandData = nullptr; const cJSON *commands = cJSON_GetObjectItem(parsedJson, "commands"); + + // I wrote it in a way that only the first command will be executed + // merge the results of all commands and then return, god damn it cJSON_ArrayForEach(commandData, commands) { const cJSON *commandTypeString = cJSON_GetObjectItem(commandData, "command"); + if (commandTypeString == nullptr) + { + return CommandResult::getErrorResult("Unknown command - missing command type"); + } + const cJSON *commandPayload = cJSON_GetObjectItem(commandData, "data"); auto commandType = commandTypeMap.at(std::string(commandTypeString->valuestring)); - std::string commandPayloadString = std::string(cJSON_Print(commandPayload)); - auto command = createCommand(commandType, commandPayloadString); + std::string commandPayloadString = ""; + if (commandPayload != nullptr) + { + std::string commandPayloadString = std::string(cJSON_Print(commandPayload)); + } + auto command = createCommand(commandType, commandPayloadString); if (command == nullptr) { cJSON_Delete(parsedJson);