From f75e44ae956992bf79e7a2af45431cfccaae54e0 Mon Sep 17 00:00:00 2001 From: Sakthivel Samidurai Date: Fri, 5 Dec 2014 13:26:52 -0300 Subject: [PATCH] Fix klocwork issues in C samples Scan klocwork and fix all issues in c samples that were highlighted. Change-Id: Id018abfd380c8723842a24f20c8f781dca177458 Signed-off-by: Sakthivel Samidurai --- .../samples/linux/SimpleClientServer/occlient.cpp | 18 +++++++++++------- .../linux/SimpleClientServer/occlientbasicops.cpp | 17 ++++++++++------- .../samples/linux/SimpleClientServer/occlientcoll.cpp | 6 +++++- .../samples/linux/SimpleClientServer/occlientslow.cpp | 17 ++++++++++------- .../samples/linux/SimpleClientServer/ocserver.cpp | 15 ++++++++++++--- .../linux/SimpleClientServer/ocserverbasicops.cpp | 8 ++++++-- .../samples/linux/SimpleClientServer/ocservercoll.cpp | 7 +++---- 7 files changed, 57 insertions(+), 31 deletions(-) diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp index 095647c..9f70f52 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp @@ -162,18 +162,22 @@ OCStackApplicationResult deleteReqCB(void *ctx, OCDoHandle handle, OCClientRespo return OC_STACK_DELETE_TRANSACTION; } -OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { +OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) +{ + if(clientResponse == NULL) + { + OC_LOG(INFO, TAG, "The clientResponse is NULL"); + return OC_STACK_DELETE_TRANSACTION; + } if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully"); } - if(clientResponse) - { - OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); - OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); - OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload); - } + OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); + OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); + OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload); + if(clientResponse->rcvdVendorSpecificHeaderOptions && clientResponse->numRcvdVendorSpecificHeaderOptions) { diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/occlientbasicops.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/occlientbasicops.cpp index 7ae7d29..a526935 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/occlientbasicops.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/occlientbasicops.cpp @@ -115,18 +115,21 @@ OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientRespons OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { + if(clientResponse == NULL) + { + OC_LOG(INFO, TAG, "The clientResponse is NULL"); + return OC_STACK_DELETE_TRANSACTION; + } if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully"); } - if(clientResponse) - { - OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); - OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); - OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", - clientResponse->resJSONPayload); - } + OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); + OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); + OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", + clientResponse->resJSONPayload); + if(clientResponse->rcvdVendorSpecificHeaderOptions && clientResponse->numRcvdVendorSpecificHeaderOptions) { diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/occlientcoll.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/occlientcoll.cpp index 523057b..e8db15a 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/occlientcoll.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/occlientcoll.cpp @@ -120,7 +120,11 @@ void PrintUsage() } OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { - if(clientResponse) {} + if(clientResponse == NULL) + { + OC_LOG(INFO, TAG, "The clientResponse is NULL"); + return OC_STACK_DELETE_TRANSACTION; + } if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully"); OC_LOG_V(INFO, TAG, "JSON = %s =============> Discovered", clientResponse->resJSONPayload); diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/occlientslow.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/occlientslow.cpp index 5fc9108..689b015 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/occlientslow.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/occlientslow.cpp @@ -83,18 +83,21 @@ OCStackResult InvokeOCDoResource(std::ostringstream &query, OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { + if(clientResponse == NULL) + { + OC_LOG(INFO, TAG, "The clientResponse is NULL"); + return OC_STACK_DELETE_TRANSACTION; + } if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully"); } - if(clientResponse) - { - OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); - OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); - OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", - clientResponse->resJSONPayload); - } + OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); + OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); + OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", + clientResponse->resJSONPayload); + if(clientResponse->rcvdVendorSpecificHeaderOptions && clientResponse->numRcvdVendorSpecificHeaderOptions) { diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp index 061bb81..1bed2bf 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp @@ -30,6 +30,9 @@ #include "cJSON.h" #include "ocserver.h" +//string length of "/a/light/" + std::numeric_limits::digits10 + '\0'" +// 9 + 9 + 1 = 19 +const int URI_MAXSIZE = 19; static int gObserveNotifyType = 3; @@ -168,8 +171,9 @@ OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest, OCE if (gCurrLightInstance < SAMPLE_MAX_NUM_POST_INSTANCE) { // Create new Light instance - char newLightUri[15] = "/a/light/"; - sprintf (newLightUri + strlen(newLightUri), "%d", gCurrLightInstance); + char newLightUri[URI_MAXSIZE]; + snprintf(newLightUri, URI_MAXSIZE, "/a/light/%d", gCurrLightInstance); + json = cJSON_CreateObject(); cJSON_AddStringToObject(json,"href",gResourceUri); cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject()); @@ -234,6 +238,11 @@ OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest, OCE OCEntityHandlerResult ProcessDeleteRequest (OCEntityHandlerRequest *ehRequest, char *payload, uint16_t maxPayloadSize) { + if(ehRequest == NULL) + { + OC_LOG(INFO, TAG, "The ehRequest is NULL"); + return OC_EH_ERROR; + } OCEntityHandlerResult ehResult = OC_EH_OK; OC_LOG_V(INFO, TAG, "\n\nExecuting %s for resource %d ", __func__, ehRequest->resource); @@ -599,7 +608,7 @@ void *ChangeLightRepresentation (void *param) uint8_t numNotifies = (SAMPLE_MAX_NUM_OBSERVATIONS)/2; OCObservationId obsNotify[numNotifies]; - while (1) + while (!gQuitFlag) { sleep(10); Light.power += 5; diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserverbasicops.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserverbasicops.cpp index e7f092b..bcd741c 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserverbasicops.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserverbasicops.cpp @@ -29,6 +29,10 @@ #include "cJSON.h" #include "ocserverbasicops.h" +//string length of "/a/led/" + std::numeric_limits::digits10 + '\0'" +// 7 + 9 + 1 = 17 +const int URI_MAXSIZE = 17; + volatile sig_atomic_t gQuitFlag = 0; static LEDResource LED; @@ -148,8 +152,8 @@ OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest, cha if (gCurrLedInstance < SAMPLE_MAX_NUM_POST_INSTANCE) { // Create new LED instance - char newLedUri[15] = "/a/led/"; - sprintf (newLedUri + strlen(newLedUri), "%d", gCurrLedInstance); + char newLedUri[URI_MAXSIZE ]; + snprintf(newLedUri, URI_MAXSIZE, "/a/led/%d", gCurrLedInstance); json = cJSON_CreateObject(); diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/ocservercoll.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/ocservercoll.cpp index 364d7a1..75124bb 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/ocservercoll.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/ocservercoll.cpp @@ -421,13 +421,14 @@ void *ChangeLightRepresentation (void *param) (void)param; OCStackResult result = OC_STACK_ERROR; - while (1) + while (!gQuitFlag) { sleep(10); light.power += 5; if (gLightUnderObservation) { - OC_LOG_V(INFO, TAG, " =====> Notifying stack of new power level %d\n", light.power); + OC_LOG_V(INFO, TAG, + " =====> Notifying stack of new power level %d\n", light.power); result = OCNotifyAllObservers (light.handle, OC_NA_QOS); if (OC_STACK_NO_OBSERVERS == result) { @@ -570,5 +571,3 @@ void createResources() res = OCBindResource(room, fan); OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res)); } - - -- 2.7.4