Fix klocwork issues in C samples
authorSakthivel Samidurai <sakthivel.samidurai@intel.com>
Fri, 5 Dec 2014 16:26:52 +0000 (13:26 -0300)
committerSakthivel Samidurai <sakthivel.samidurai@intel.com>
Tue, 9 Dec 2014 12:06:09 +0000 (09:06 -0300)
Scan klocwork and fix all issues in c samples that were highlighted.

Change-Id: Id018abfd380c8723842a24f20c8f781dca177458
Signed-off-by: Sakthivel Samidurai <sakthivel.samidurai@intel.com>
resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp
resource/csdk/stack/samples/linux/SimpleClientServer/occlientbasicops.cpp
resource/csdk/stack/samples/linux/SimpleClientServer/occlientcoll.cpp
resource/csdk/stack/samples/linux/SimpleClientServer/occlientslow.cpp
resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp
resource/csdk/stack/samples/linux/SimpleClientServer/ocserverbasicops.cpp
resource/csdk/stack/samples/linux/SimpleClientServer/ocservercoll.cpp

index 095647c..9f70f52 100644 (file)
@@ -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)
     {
index 7ae7d29..a526935 100644 (file)
@@ -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)
     {
index 523057b..e8db15a 100644 (file)
@@ -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);
index 5fc9108..689b015 100644 (file)
@@ -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)
     {
index 061bb81..1bed2bf 100644 (file)
@@ -30,6 +30,9 @@
 #include "cJSON.h"
 #include "ocserver.h"
 
+//string length of "/a/light/" + std::numeric_limits<int>::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;
index e7f092b..bcd741c 100644 (file)
 #include "cJSON.h"
 #include "ocserverbasicops.h"
 
+//string length of "/a/led/" + std::numeric_limits<int>::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();
 
index 364d7a1..75124bb 100644 (file)
@@ -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));
 }
-
-