From fd7158c7149d41a9999b403b46fee824a9ad583b Mon Sep 17 00:00:00 2001 From: rashmy Date: Thu, 27 Sep 2018 16:43:48 -0700 Subject: [PATCH] reusing some variables --- ...iner_ContainerInventory_Class_Provider.cpp | 26 ++++++++++++------- .../Container_DaemonEvent_Class_Provider.cpp | 5 ++-- ...ontainer_ImageInventory_Class_Provider.cpp | 16 +++++++----- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/source/code/providers/Container_ContainerInventory_Class_Provider.cpp b/source/code/providers/Container_ContainerInventory_Class_Provider.cpp index 68c13053a..ded8fb869 100644 --- a/source/code/providers/Container_ContainerInventory_Class_Provider.cpp +++ b/source/code/providers/Container_ContainerInventory_Class_Provider.cpp @@ -210,7 +210,11 @@ class ContainerQuery correctedstring = stringToTruncate + "\"]"; } instance.EnvironmentVar_value(correctedstring.c_str()); - syslog(LOG_WARNING, "Environment variable truncated for container %s", cJSON_GetObjectItem(entry, "Id")->valuestring); + cJSON* idItem = cJSON_GetObjectItem(entry, "Id"); + if (idItem != NULL) + { + syslog(LOG_WARNING, "Environment variable truncated for container %s", idItem->valuestring); + } } else { instance.EnvironmentVar_value(strcmp(env, "null") ? env : ""); @@ -244,9 +248,10 @@ class ContainerQuery } else { - if (cJSON_GetObjectItem(entry, "Id") != NULL) + cJSON* idItem = cJSON_GetObjectItem(entry, "Id"); + if (idItem != NULL) { - syslog(LOG_WARNING, "Attempt in ObtainContainerConfig to get container %s config information returned null", cJSON_GetObjectItem(entry, "Id")->valuestring); + syslog(LOG_WARNING, "Attempt in ObtainContainerConfig to get container %s config information returned null", idItem->valuestring); } } } @@ -281,9 +286,10 @@ class ContainerQuery if (exitCode < 0) { exitCode = 128; - if (cJSON_GetObjectItem(entry, "Id") != NULL) + cJSON* idItem = cJSON_GetObjectItem(entry, "Id"); + if (idItem != NULL) { - syslog(LOG_NOTICE, "Container %s returned negative exit code", cJSON_GetObjectItem(entry, "Id")->valuestring); + syslog(LOG_NOTICE, "Container %s returned negative exit code", idItem->valuestring); } } @@ -334,9 +340,10 @@ class ContainerQuery } else { - if (cJSON_GetObjectItem(entry, "Id")) + cJSON* idItem = cJSON_GetObjectItem(entry, "Id"); + if (idItem) { - syslog(LOG_WARNING, "Attempt in ObtainContainerState to get container %s state information returned null", cJSON_GetObjectItem(entry, "Id")->valuestring); + syslog(LOG_WARNING, "Attempt in ObtainContainerState to get container %s state information returned null", idItem->valuestring); } } } @@ -381,9 +388,10 @@ class ContainerQuery } else { - if (cJSON_GetObjectItem(entry, "Id")) + cJSON* idItem = cJSON_GetObjectItem(entry, "Id"); + if (idItem != NULL) { - syslog(LOG_WARNING, "Attempt in ObtainContainerHostConfig to get container %s host config information returned null", cJSON_GetObjectItem(entry, "Id")->valuestring); + syslog(LOG_WARNING, "Attempt in ObtainContainerHostConfig to get container %s host config information returned null", idItem->valuestring); } } } diff --git a/source/code/providers/Container_DaemonEvent_Class_Provider.cpp b/source/code/providers/Container_DaemonEvent_Class_Provider.cpp index 51e253d73..0c28e4769 100644 --- a/source/code/providers/Container_DaemonEvent_Class_Provider.cpp +++ b/source/code/providers/Container_DaemonEvent_Class_Provider.cpp @@ -288,10 +288,11 @@ class EventQuery } else { + cJSON* idItem = cJSON_GetObjectItem(entry, "id"); // Image event - if (cJSON_GetObjectItem(entry, "id") != NULL) + if (idItem != NULL) { - instance.ElementName_value(cJSON_GetObjectItem(entry, "id")->valuestring); + instance.ElementName_value(idItem->valuestring); } instance.Id_value(""); instance.ContainerName_value(""); diff --git a/source/code/providers/Container_ImageInventory_Class_Provider.cpp b/source/code/providers/Container_ImageInventory_Class_Provider.cpp index 01d1c639c..f5742ef5f 100644 --- a/source/code/providers/Container_ImageInventory_Class_Provider.cpp +++ b/source/code/providers/Container_ImageInventory_Class_Provider.cpp @@ -173,10 +173,12 @@ class InventoryQuery { string id = string(objItem->valuestring); - if (cJSON_GetObjectItem(state, "Running") != NULL && cJSON_GetObjectItem(state, "Running")->valueint) + cJSON* runningItem = cJSON_GetObjectItem(state, "Running"); + if (runningItem != NULL && runningItem->valueint) { // Running container - if (cJSON_GetObjectItem(state, "Paused") != NULL && cJSON_GetObjectItem(state, "Paused")->valueint) + cJSON* pausedItem = cJSON_GetObjectItem(state, "Paused"); + if (pausedItem != NULL && pausedItem->valueint) { // Paused container instances[idTable[id]].Paused_value(instances[idTable[id]].Paused_value() + 1); @@ -188,7 +190,8 @@ class InventoryQuery } else { - if (cJSON_GetObjectItem(state, "ExitCode") != NULL && cJSON_GetObjectItem(state, "ExitCode")->valueint) + cJSON* exitCodeItem = cJSON_GetObjectItem(state, "ExitCode"); + if (exitCodeItem != NULL && exitCodeItem->valueint) { // Container exited nonzero instances[idTable[id]].Failed_value(instances[idTable[id]].Failed_value() + 1); @@ -206,9 +209,10 @@ class InventoryQuery } else { - if (cJSON_GetObjectItem(entry, "Id") != NULL) + cJSON* idItem = cJSON_GetObjectItem(entry, "Id"); + if (idItem != NULL) { - syslog(LOG_WARNING, "Attempt in ObtainContainerState to get container %s state information returned null", cJSON_GetObjectItem(entry, "Id")->valuestring); + syslog(LOG_WARNING, "Attempt in ObtainContainerState to get container %s state information returned null", idItem->valuestring); } } } @@ -263,7 +267,7 @@ class InventoryQuery } else { - syslog(LOG_WARNING, "API call in AggregateContainerStatus to inspect container %s returned null", cJSON_GetObjectItem(entry, "Id")->valuestring); + syslog(LOG_WARNING, "API call in AggregateContainerStatus to inspect container %s returned null", objItem->valuestring); } } }