Fix issues detected by static analysis
authorGlen Youngjin Kim <glen.kim@samsung.com>
Thu, 18 May 2017 12:36:49 +0000 (21:36 +0900)
committerUze Choi <uzchoi@samsung.com>
Sun, 21 May 2017 05:36:17 +0000 (05:36 +0000)
Fixed memory leak issue on occlientcb.c, occollection.c, ocpayload.c
and ocresource.c.
Fixed dereference issue without checking NULL on ocpayloadparse.c

Change-Id: Ibf1757ca85833c2f1b849354e93882730c6c78f9
Signed-off-by: Glen Youngjin Kim <glen.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/20045
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Jaewook Jung <jw0213.jung@samsung.com>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
resource/csdk/stack/src/occlientcb.c
resource/csdk/stack/src/occollection.c
resource/csdk/stack/src/ocpayloadparse.c
resource/csdk/stack/src/ocresource.c

index 32b0e96..57d4873 100755 (executable)
@@ -98,6 +98,8 @@ AddClientCB(ClientCB** clientCB, OCCallbackData* cbData,
                 if (!cbNode->options)
                 {
                     OIC_LOG(ERROR, TAG, "Out of memory");
+                    OICFree(cbNode);
+
                     return OC_STACK_NO_MEMORY;
                 }
                 memcpy(cbNode->options, options,
@@ -121,6 +123,8 @@ AddClientCB(ClientCB** clientCB, OCCallbackData* cbData,
                     {
                         OICFree(cbNode->options);
                     }
+                    OICFree(cbNode);
+
                     return OC_STACK_NO_MEMORY;
                 }
                 memcpy(cbNode->payload, payload, payloadSize);
index 5a19e15..13d44d8 100755 (executable)
@@ -80,7 +80,18 @@ static bool AddRTSBaslinePayload(OCRepPayload **linkArray, int size, OCRepPayloa
         rts[k++] = OICStrdup(rsrcType->value);
     }
 
-    return OCRepPayloadSetStringArrayAsOwner(*colPayload, OC_RSRVD_RTS, rts, dim);
+    bool b = OCRepPayloadSetStringArrayAsOwner(*colPayload, OC_RSRVD_RTS, rts, dim);
+
+    if (!b)
+    {
+        for (size_t j = 0; j < arraySize; j++)
+        {
+            OICFree(rts[j]);
+        }
+        OICFree(rts);
+    }
+
+    return b;
 }
 
 static OCStackResult SendResponse(const OCRepPayload *payload, const OCEntityHandlerRequest *ehRequest,
@@ -408,7 +419,14 @@ OCStackResult BuildCollectionLinksPayloadValue(const char* resourceUri, OCRepPay
         if (!arrayPayload[i])
         {
             OICFree(createdPayloadValue);
+
+            for (size_t j = 0; j < i; j++)
+            {
+                OCRepPayloadDestroy(arrayPayload[j]);
+            }
+
             OICFree(arrayPayload);
+
             return result;
         }
 
@@ -430,6 +448,15 @@ OCStackResult BuildCollectionLinksPayloadValue(const char* resourceUri, OCRepPay
         if (!OCRepPayloadSetPropObjectAsOwner(arrayPayload[i], OC_RSRVD_POLICY,
                              addPolicyPayload((OCResourceHandle*)iterResource, devAddr)))
         {
+            OICFree(createdPayloadValue);
+
+            for (size_t j = 0; j <= i; j++)
+            {
+                OCRepPayloadDestroy(arrayPayload[j]);
+            }
+
+            OICFree(arrayPayload);
+
             return result;
         }
 
index f228a36..2e8f728 100755 (executable)
@@ -563,6 +563,8 @@ static CborError ParseResources(OCDiscoveryPayload **outPayload, CborValue *reso
         else
         {
             char *di = OICStrdup(resource->anchor + 6);
+            VERIFY_PARAM_NON_NULL(TAG, di, "Failed to duplicating di");
+
             char *slash = strchr(di, '/');
             if (slash)
             {
index 69ae755..4b9ef04 100755 (executable)
@@ -498,7 +498,17 @@ OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
             OIC_LOG_V(DEBUG, TAG, "value: %s", value);
             itf[i] = OICStrdup(value);
         }
-        OCRepPayloadSetStringArrayAsOwner(tempPayload, OC_RSRVD_INTERFACE, itf, ifDim);
+
+        bool b = OCRepPayloadSetStringArrayAsOwner(tempPayload, OC_RSRVD_INTERFACE, itf, ifDim);
+
+        if (!b)
+        {
+            for (uint8_t i = 0; i < numElement; i++)
+            {
+                OICFree(itf[i]);
+            }
+            OICFree(itf);
+        }
     }
 
     for (OCAttribute *resAttrib = resourcePtr->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)