CONPRO-1320] Cleanup observers of deleted resource. 81/190681/1
authorAmit KS <amit.s12@samsung.com>
Thu, 4 Oct 2018 15:00:41 +0000 (20:30 +0530)
committerAmit KS <amit.s12@samsung.com>
Thu, 4 Oct 2018 15:05:38 +0000 (20:35 +0530)
Observers were not cleane up for deleted resource, this
caused crash in trying to access deleted resource while
deleting these observers when TCP connection is closed.
Changes are made to clean up the observers while deleting
resource itself

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/318
(cherry picked from commit a7f8adbd8574db8b22a9fbf2340721ebac4c148e)

Signed-off-by: Harish Kumara M <h.marappa@samsung.com>
Signed-off-by: Amit KS <amit.s12@samsung.com>
Change-Id: I2860a6aaa6324b3ff55070860e8e6b0090f6d2eb

resource/csdk/stack/include/internal/ocobserve.h
resource/csdk/stack/src/ocobserve.c
resource/csdk/stack/src/ocstack.c

index 2fa6724..f5fc6c1 100644 (file)
@@ -214,6 +214,15 @@ OCStackResult AddObserver (const char         *resUri,
 OCStackResult DeleteObserverUsingDevAddr(const OCDevAddr *devAddr);
 
 /**
+ * Delete all observers of a resource.
+ *
+ * @param resource Handle of resource whose observers needs to be deleted.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+OCStackResult DeleteObserverUsingResource(OCResource *res);
+
+/**
  * Search the list of observers for the specified token.
  *
  * @param token            Token to search for.
index 9fde469..18de8c6 100644 (file)
@@ -767,6 +767,30 @@ OCStackResult DeleteObserverUsingDevAddr(const OCDevAddr *devAddr)
     return OC_STACK_OK;
 }
 
+OCStackResult DeleteObserverUsingResource(OCResource *res)
+{
+    if (!res)
+    {
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    oc_mutex_lock(g_serverObsListMutex);
+    ResourceObserver *obs = NULL;
+    ResourceObserver *next = NULL;
+    LL_FOREACH_SAFE(g_serverObsList, obs, next)
+    {
+        if (obs->resource == res)
+        {
+            OIC_LOG_V(INFO, TAG, "Deleting observer: id-%u, token-", obs->observeId);
+            OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)obs->token, obs->tokenLength);
+            LL_DELETE(g_serverObsList, obs);
+            FreeObserver(obs);
+        }
+    }
+    oc_mutex_unlock(g_serverObsListMutex);
+    return OC_STACK_OK;
+}
+
 void DeleteObserverList()
 {
     oc_mutex_lock(g_serverObsListMutex);
index f469c51..139ff3a 100755 (executable)
@@ -4649,6 +4649,9 @@ OCStackResult deleteResource(OCResource *resource)
                 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_DELETE);
             }
 #endif
+            // Delete resource's all observers
+            DeleteObserverUsingResource(resource);
+
             // Only resource in list.
             if (temp == headResource && temp == tailResource)
             {
@@ -4717,6 +4720,8 @@ void deleteResourceElements(OCResource *resource)
         OCDeleteResourceAttributes(resource->rsrcAttributes);
         resource->rsrcAttributes = NULL;
     }
+
+    resource->entityHandler = NULL;
     resource = NULL;
 }