Added check to not add duplicate resourcetype.
authorMandeep Shetty <mandeep.shetty@intel.com>
Thu, 26 Mar 2015 00:16:05 +0000 (17:16 -0700)
committerErich Keane <erich.keane@intel.com>
Thu, 26 Mar 2015 20:07:21 +0000 (20:07 +0000)
Do not add already existing resourcetype and or resource interface to linked list.
Also fixed incorrect logic in insertResourceType().

Change-Id: I43195e8f9d0d6c71274fd7321baf3a02268e2cf2
Signed-off-by: Mandeep Shetty <mandeep.shetty@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/574
Reviewed-by: Joseph Morrow <joseph.l.morrow@intel.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/csdk/stack/src/ocstack.c

index f12111a..7077b55 100644 (file)
@@ -2255,8 +2255,6 @@ OCStackResult BindResourceTypeToResource(OCResource* resource,
     // TODO:  Does resource attribute resentation really have to be maintained in stack?
     // Is it presented during resource discovery?
 
-    //TODO ("Make sure that the resourcetypename doesn't already exist in the resource");
-
     // Create the resourcetype and insert it into the resource list
     pointer = (OCResourceType *) OCCalloc(1, sizeof(OCResourceType));
     if (!pointer)
@@ -3273,33 +3271,43 @@ void deleteResourceInterface(OCResourceInterface *resourceInterface)
 
 /**
  * Insert a resource type into a resource's resource type linked list.
- *
+ * If resource type already exists, it will not be inserted and the
+ * resourceType will be free'd.
+ * resourceType->next should be null to avoid memory leaks.
+ * Function returns silently for null args..
  * @param resource - resource where resource type is to be inserted
  * @param resourceType - resource type to be inserted
  */
 void insertResourceType(OCResource *resource, OCResourceType *resourceType)
 {
     OCResourceType *pointer = NULL;
-
-    if (resource && !resource->rsrcType)
+    OCResourceType *previous = NULL;
+    if (!resource || !resourceType)
+    {
+        return;
+    }
+    // resource type list is empty.
+    else if (!resource->rsrcType)
     {
         resource->rsrcType = resourceType;
     }
     else
     {
-        if(resource)
-        {
-            pointer = resource->rsrcType;
-        }
-        else
-        {
-            pointer = resourceType;
-        }
-        while (pointer->next)
+        pointer = resource->rsrcType;
+
+        while (pointer)
         {
+            // resource type already exists. Free 2nd arg and return.
+            if (!strcmp(resourceType->resourcetypename, pointer->resourcetypename))
+            {
+                OCFree(resourceType->resourcetypename);
+                OCFree(resourceType);
+                return;
+            }
+            previous = pointer;
             pointer = pointer->next;
         }
-        pointer->next = resourceType;
+        previous->next = resourceType;
     }
     resourceType->next = NULL;
 }
@@ -3372,7 +3380,9 @@ OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char *
 }
 /**
  * Insert a resource interface into a resource's resource interface linked list.
- *
+ * If resource interface already exists, it will not be inserted and the
+ * resourceInterface will be free'd.
+ * resourceInterface->next should be null to avoid memory leaks.
  * @param resource - resource where resource interface is to be inserted
  * @param resourceInterface - resource interface to be inserted
  */
@@ -3380,6 +3390,8 @@ void insertResourceInterface(OCResource *resource,
         OCResourceInterface *resourceInterface)
 {
     OCResourceInterface *pointer = NULL;
+    OCResourceInterface *previous = NULL;
+
     if (!resource->rsrcInterface)
     {
         resource->rsrcInterface = resourceInterface;
@@ -3387,11 +3399,19 @@ void insertResourceInterface(OCResource *resource,
     else
     {
         pointer = resource->rsrcInterface;
-        while (pointer->next)
+        while (pointer)
         {
+            // resource type already exists. Free 2nd arg and return.
+            if (!strcmp(resourceInterface->name, pointer->name))
+            {
+                OCFree(resourceInterface->name);
+                OCFree(resourceInterface);
+                return;
+            }
+            previous = pointer;
             pointer = pointer->next;
         }
-        pointer->next = resourceInterface;
+        previous->next = resourceInterface;
     }
     resourceInterface->next = NULL;
 }