freed payload in OCSendKeepAliveRequest 14/194514/1
authorsamanway-dey <samanway.dey@samsung.com>
Tue, 27 Nov 2018 16:35:00 +0000 (22:05 +0530)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Wed, 5 Dec 2018 06:50:24 +0000 (15:50 +0900)
https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/346
(cherry picked from commit 80cddcb478a802a2d7b6705eeb0624caca15d660)

Change-Id: Id86c7fce30d9f5a2fa137ac6ca8be89005ab3c29
Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
resource/csdk/stack/include/oickeepalive.h
resource/csdk/stack/src/oickeepalive.c
resource/src/InProcClientWrapper.cpp

index d813665..9aeeeac 100644 (file)
@@ -65,7 +65,8 @@ OCStackResult OCFindKeepAliveResource(OCDoHandle *handle, const char *remoteAddr
  *                          the consumer.  A NULL handle is permitted in the event where the caller
  *                          has no use for the return value.
  * @param remoteAddr        The target device address to discovery a resource.
- * @param payload           Encoded request payload.
+ * @param payload           Encoded request payload.This API takes ownership of payload passed as input,
+ *                          so whenever it returns, payload memory is de-allocated.
  * @param cbData            Asynchronous callback function that is invoked by the stack when
  *                          discovery or resource interaction is received. The discovery could be
  *                          related to filtered/scoped/particular resource. The callback is
index 1340b70..7c29f72 100644 (file)
@@ -59,6 +59,9 @@ static const uint64_t USECS_PER_SEC = 1000000;
 #define VERIFY_NON_NULL_V(arg) { if (!arg) {OIC_LOG_V(FATAL, TAG, "%s is NULL", #arg);\
     goto exit;} }
 
+#define VERIFY_NON_NULL_EXIT(arg, logLevel, retVal) { if (!(arg)) { OIC_LOG((logLevel), \
+             TAG, #arg " is NULL"); ret = retVal; goto exit; } }
+
 /**
  * The KeepAlive table entries are removed
  * if it can't receive response message within 60 seconds.
@@ -276,10 +279,11 @@ OCStackResult OCFindKeepAliveResource(OCDoHandle *handle, const char *remoteAddr
 OCStackResult OCSendKeepAliveRequest(OCDoHandle *handle, const char *remoteAddr,
                                      OCPayload *payload, OCCallbackData *cbData)
 {
-    VERIFY_NON_NULL(remoteAddr, FATAL, OC_STACK_INVALID_PARAM);
-    VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_PARAM);
-    VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
-    VERIFY_NON_NULL(payload, FATAL, OC_STACK_INVALID_CALLBACK);
+    OCStackResult ret;
+    VERIFY_NON_NULL_EXIT(remoteAddr, FATAL, OC_STACK_INVALID_PARAM);
+    VERIFY_NON_NULL_EXIT(cbData, FATAL, OC_STACK_INVALID_PARAM);
+    VERIFY_NON_NULL_EXIT(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
+    VERIFY_NON_NULL_EXIT(payload, FATAL, OC_STACK_INVALID_CALLBACK);
 
     OIC_LOG(DEBUG, TAG, "SendKeepAliveRequest IN");
 
@@ -292,15 +296,17 @@ OCStackResult OCSendKeepAliveRequest(OCDoHandle *handle, const char *remoteAddr,
     if (result != OC_STACK_OK)
     {
         OIC_LOG_V(DEBUG, TAG, "Unable to parse uri: %s", remoteAddr);
+        OCPayloadDestroy(payload);
         return OC_STACK_ERROR;
     }
 
-    VERIFY_NON_NULL(devAddr, FATAL, OC_STACK_INVALID_PARAM);
+    VERIFY_NON_NULL_EXIT(devAddr, FATAL, OC_STACK_INVALID_PARAM);
 
     if (!(devAddr->adapter & OC_ADAPTER_TCP))
     {
         OIC_LOG_V(DEBUG, TAG, "Not supported connectivity type");
         OICFree(devAddr);
+        OCPayloadDestroy(payload);
         return OC_STACK_ERROR;
     }
 
@@ -320,6 +326,7 @@ OCStackResult OCSendKeepAliveRequest(OCDoHandle *handle, const char *remoteAddr,
             oc_mutex_unlock(g_mutexObjectList);
             OIC_LOG(ERROR, TAG, "Failed to add new KeepAlive entry");
             OICFree(devAddr);
+            OCPayloadDestroy(payload);
             return OC_STACK_ERROR;
         }
     }
@@ -334,6 +341,7 @@ OCStackResult OCSendKeepAliveRequest(OCDoHandle *handle, const char *remoteAddr,
             oc_mutex_unlock(g_mutexObjectList);
             OIC_LOG(ERROR, TAG, "Already sent a ping request to remote device");
             OICFree(devAddr);
+            OCPayloadDestroy(payload);
             return OC_STACK_ERROR;
         }
         entry->interval = interval;
@@ -381,6 +389,10 @@ OCStackResult OCSendKeepAliveRequest(OCDoHandle *handle, const char *remoteAddr,
     OIC_LOG(DEBUG, TAG, "SendKeepAliveRequest OUT");
     OICFree(devAddr);
     return result;
+
+exit:
+    OCPayloadDestroy(payload);
+    return (ret);
 }
 
 OCStackResult OCHandleKeepAliveResponse(const CAEndpoint_t *endPoint, const OCPayload *payload)
index 41bb8fa..75b7b46 100644 (file)
@@ -1823,10 +1823,6 @@ namespace OC
             std::lock_guard<std::recursive_mutex> lock(*cLock);
             OCRepPayload *payload = rep.getPayload();
             result = OCSendKeepAliveRequest (nullptr, host.c_str(), (OCPayload*)payload, &cbdata);
-            if (result != OC_STACK_OK)
-            {
-                OCRepPayloadDestroy(payload);
-            }
         }
         else
         {