Bug fix about memory leak. 43/13243/2
authorjaesick.shin <jaesick.shin@samsung.com>
Thu, 13 Oct 2016 15:31:53 +0000 (00:31 +0900)
committerUze Choi <uzchoi@samsung.com>
Fri, 14 Oct 2016 01:18:48 +0000 (01:18 +0000)
this patch include,
bug fix for dynamic analyzer.

Change-Id: I9a36d496b76c9d712d80af45492f0e38a7aff4ea
Signed-off-by: jaesick.shin <jaesick.shin@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/13243
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/notification/src/provider/NSProviderCallbackResponse.c
service/notification/src/provider/NSProviderListener.c
service/notification/src/provider/NSProviderMemoryCache.c
service/notification/src/provider/NSProviderSubscription.c
service/notification/src/provider/NSProviderTopic.c

index a450959..e079f8c 100644 (file)
@@ -90,15 +90,15 @@ void * NSCallbackResponseSchedule(void * ptr)
                     OCEntityHandlerRequest * request = (OCEntityHandlerRequest*)node->taskData;\r
                     NSConsumer * consumer = (NSConsumer *)OICMalloc(sizeof(NSConsumer));\r
 \r
-                    char * consumerId = NSGetValueFromQuery(OICStrdup(request->query),\r
-                            NS_QUERY_CONSUMER_ID);\r
+                    char * copyQuery = OICStrdup(request->query);\r
+                    char * consumerId = NSGetValueFromQuery(copyQuery, NS_QUERY_CONSUMER_ID);\r
 \r
                     if(consumerId)\r
                     {\r
                         OICStrcpy(consumer->consumerId, UUID_STRING_SIZE, consumerId);\r
                         NSSubscribeRequestCb(consumer);\r
                     }\r
-\r
+                    OICFree(copyQuery);\r
                     NSFreeConsumer(consumer);\r
                     NSFreeOCEntityHandlerRequest(request);\r
 \r
index ae623c6..dfd28e7 100644 (file)
@@ -49,16 +49,18 @@ OCEntityHandlerResult NSEntityHandlerNotificationCb(OCEntityHandlerFlag flag,
         {\r
             NS_LOG(DEBUG, "NSEntityHandlerNotificationCb - OC_REST_GET");\r
 \r
-            char * reqInterface =\r
-                    NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_INTERFACE);\r
+            char * copyQuery = OICStrdup(entityHandlerRequest->query);\r
+            char * reqInterface = NSGetValueFromQuery(copyQuery, NS_QUERY_INTERFACE);\r
 \r
             if (reqInterface && strcmp(reqInterface, NS_INTERFACE_BASELINE) != 0\r
                     && strcmp(reqInterface, NS_INTERFACE_READ) != 0)\r
             {\r
                 NS_LOG(ERROR, "Invalid interface");\r
+                OICFree(copyQuery);\r
                 return ehResult;\r
             }\r
 \r
+            OICFree(copyQuery);\r
             NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_POLICY,\r
                     NSCopyOCEntityHandlerRequest(entityHandlerRequest));\r
 \r
@@ -100,8 +102,9 @@ OCEntityHandlerResult NSEntityHandlerMessageCb(OCEntityHandlerFlag flag,
         {\r
             NS_LOG(DEBUG, "NSEntityHandlerMessageCb - OC_REST_GET");\r
 \r
-            reqInterface =\r
-                    NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_INTERFACE);\r
+            char * copyQuery = OICStrdup(entityHandlerRequest->query);\r
+            reqInterface = OICStrdup(NSGetValueFromQuery(copyQuery, NS_QUERY_INTERFACE));\r
+            OICFree(copyQuery);\r
 \r
             if (reqInterface && strcmp(reqInterface, NS_INTERFACE_BASELINE) != 0\r
                     && strcmp(reqInterface, NS_INTERFACE_READ) != 0)\r
@@ -109,7 +112,6 @@ OCEntityHandlerResult NSEntityHandlerMessageCb(OCEntityHandlerFlag flag,
                 NS_LOG(ERROR, "Invalid interface");\r
                 return ehResult;\r
             }\r
-\r
             ehResult = OC_EH_OK;\r
         }\r
         else\r
@@ -129,6 +131,7 @@ OCEntityHandlerResult NSEntityHandlerMessageCb(OCEntityHandlerFlag flag,
             NS_LOG(DEBUG, "NSEntityHandlerMessageCb - OC_OBSERVE_REGISTER");\r
             NS_LOG_V(DEBUG, "NSEntityHandlerMessageCb\n"\r
                     "Register message observerID : %d\n", entityHandlerRequest->obsInfo.obsId);\r
+\r
             NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_RECV_SUBSCRIPTION,\r
                     NSCopyOCEntityHandlerRequest(entityHandlerRequest));\r
             ehResult = OC_EH_OK;\r
@@ -149,9 +152,11 @@ OCEntityHandlerResult NSEntityHandlerMessageCb(OCEntityHandlerFlag flag,
         }\r
     }\r
 \r
-    NS_LOG(DEBUG, "NSEntityHandlerMessageCb - OUT");\r
-    return NSProviderSendResponse(entityHandlerRequest, payload, reqInterface, ehResult,\r
+    ehResult = NSProviderSendResponse(entityHandlerRequest, payload, reqInterface, ehResult,\r
             NS_INTERFACE_TYPE_READ, NS_RESOURCE_MESSAGE);\r
+    OICFree(reqInterface);\r
+    NS_LOG(DEBUG, "NSEntityHandlerMessageCb - OUT");\r
+    return ehResult;\r
 }\r
 \r
 OCEntityHandlerResult NSEntityHandlerSyncCb(OCEntityHandlerFlag flag,\r
@@ -176,8 +181,10 @@ OCEntityHandlerResult NSEntityHandlerSyncCb(OCEntityHandlerFlag flag,
 \r
         if (OC_REST_GET == entityHandlerRequest->method)\r
         {\r
-            reqInterface =\r
-                    NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_INTERFACE);\r
+\r
+            char * copyQuery = OICStrdup(entityHandlerRequest->query);\r
+            reqInterface = OICStrdup(NSGetValueFromQuery(copyQuery, NS_QUERY_INTERFACE));\r
+            OICFree(copyQuery);\r
 \r
             if (reqInterface && strcmp(reqInterface, NS_INTERFACE_BASELINE) != 0\r
                     && strcmp(reqInterface, NS_INTERFACE_READWRITE) != 0)\r
@@ -237,8 +244,12 @@ OCEntityHandlerResult NSEntityHandlerSyncCb(OCEntityHandlerFlag flag,
     }\r
 \r
     NS_LOG(DEBUG, "NSEntityHandlerSyncCb - OUT");\r
-    return NSProviderSendResponse(entityHandlerRequest, payload, reqInterface, ehResult,\r
-            NS_INTERFACE_TYPE_READWRITE, NS_RESOURCE_SYNC);\r
+\r
+    ehResult = NSProviderSendResponse(entityHandlerRequest, payload, reqInterface, ehResult,\r
+            NS_INTERFACE_TYPE_READ, NS_RESOURCE_MESSAGE);\r
+    OICFree(reqInterface);\r
+\r
+    return ehResult;\r
 }\r
 \r
 OCEntityHandlerResult NSEntityHandlerTopicCb(OCEntityHandlerFlag flag,\r
@@ -265,8 +276,9 @@ OCEntityHandlerResult NSEntityHandlerTopicCb(OCEntityHandlerFlag flag,
         {\r
             NS_LOG(DEBUG, "NSEntityHandlerTopicCb - OC_REST_GET");\r
 \r
-            reqInterface =\r
-                    NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_INTERFACE);\r
+            char * copyReq = OICStrdup(entityHandlerRequest->query);\r
+            reqInterface = OICStrdup(NSGetValueFromQuery(copyReq, NS_QUERY_INTERFACE));\r
+            OICFree(copyReq);\r
 \r
             if (reqInterface && strcmp(reqInterface, NS_INTERFACE_BASELINE) != 0\r
                     && strcmp(reqInterface, NS_INTERFACE_READWRITE) != 0)\r
@@ -307,8 +319,10 @@ OCEntityHandlerResult NSEntityHandlerTopicCb(OCEntityHandlerFlag flag,
     }\r
 \r
     NS_LOG(DEBUG, "NSEntityHandlerTopicCb - OUT");\r
-    return NSProviderSendResponse(entityHandlerRequest, payload, reqInterface, ehResult,\r
+    ehResult = NSProviderSendResponse(entityHandlerRequest, payload, reqInterface, ehResult,\r
             NS_INTERFACE_TYPE_READWRITE, NS_RESOURCE_TOPIC);\r
+    OICFree(reqInterface);\r
+    return ehResult;\r
 }\r
 \r
 void NSProviderConnectionStateListener(const CAEndpoint_t * info, bool connected)\r
index 49c2b37..2e6034a 100644 (file)
@@ -186,7 +186,8 @@ NSResult NSProviderStorageWrite(NSCacheList * list, NSCacheElement * newObj)
                 }\r
 \r
                 NS_LOG(DEBUG, "Update Data - OUT");\r
-\r
+                OICFree(subData);\r
+                OICFree(newObj);\r
                 pthread_mutex_unlock(&NSCacheMutex);\r
                 return NS_OK;\r
             }\r
index f526a46..321109d 100644 (file)
@@ -80,8 +80,8 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest
 \r
     NS_LOG_V(DEBUG, "NS Provider ID: %s", NSGetProviderInfo()->providerId);\r
 \r
-    char * reqInterface =\r
-            NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_INTERFACE);\r
+    char * copyReq = OICStrdup(entityHandlerRequest->query);\r
+    char * reqInterface = NSGetValueFromQuery(copyReq, NS_QUERY_INTERFACE);\r
 \r
     if (reqInterface && strcmp(reqInterface, NS_INTERFACE_BASELINE) == 0)\r
     {\r
@@ -89,6 +89,7 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest
         OCResourcePayloadAddStringLL(&payload->interfaces, NS_INTERFACE_READ);\r
         OCResourcePayloadAddStringLL(&payload->types, NS_ROOT_TYPE);\r
     }\r
+    OICFree(copyReq);\r
     OCRepPayloadSetUri(payload, NS_ROOT_URI);\r
     OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, NSGetProviderInfo()->providerId);\r
     OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_VERSION, VERSION);\r
@@ -121,10 +122,12 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour
 {\r
     NS_LOG(DEBUG, "NSHandleSubscription - IN");\r
 \r
-    char * id = NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_CONSUMER_ID);\r
+    char * copyReq = OICStrdup(entityHandlerRequest->query);\r
+    char * id = NSGetValueFromQuery(copyReq, NS_QUERY_CONSUMER_ID);\r
 \r
     if(!id)\r
     {\r
+        OICFree(copyReq);\r
         NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
         NS_LOG(ERROR, "Invalid ConsumerID");\r
         return;\r
@@ -237,6 +240,7 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour
 \r
         NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
     }\r
+    OICFree(copyReq);\r
 \r
     NS_LOG(DEBUG, "NSHandleSubscription - OUT");\r
 }\r
@@ -321,10 +325,12 @@ NSResult NSSendConsumerSubResponse(OCEntityHandlerRequest * entityHandlerRequest
         return NS_ERROR;\r
     }\r
 \r
-    char * id = NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_CONSUMER_ID);\r
+    char * copyReq = OICStrdup(entityHandlerRequest->query);\r
+    char * id = NSGetValueFromQuery(copyReq, NS_QUERY_CONSUMER_ID);\r
 \r
     if(!id)\r
     {\r
+        OICFree(copyReq);\r
         NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
         NS_LOG(ERROR, "Invalid ConsumerID");\r
         return NS_ERROR;\r
@@ -332,6 +338,7 @@ NSResult NSSendConsumerSubResponse(OCEntityHandlerRequest * entityHandlerRequest
 \r
     NSCacheUpdateSubScriptionState(consumerSubList, id, true);\r
     NSSendResponse(id, true);\r
+    OICFree(copyReq);\r
     NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
     NS_LOG(DEBUG, "NSSendSubscriptionResponse - OUT");\r
     return NS_OK;\r
index 3037467..36abe6d 100644 (file)
@@ -235,7 +235,8 @@ NSResult NSSendTopicList(OCEntityHandlerRequest * entityHandlerRequest)
 {\r
     NS_LOG(DEBUG, "NSSendTopicList - IN");\r
 \r
-    char * id = NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_CONSUMER_ID);\r
+    char * copyReq = OICStrdup(entityHandlerRequest->query);\r
+    char * id = NSGetValueFromQuery(copyReq, NS_QUERY_CONSUMER_ID);\r
     NSTopicLL * topics = NULL;\r
 \r
     if (!id)\r
@@ -264,6 +265,7 @@ NSResult NSSendTopicList(OCEntityHandlerRequest * entityHandlerRequest)
     if (!payload)\r
     {\r
         NS_LOG(ERROR, "payload is NULL");\r
+        OICFree(copyReq);\r
         return NS_ERROR;\r
     }\r
 \r
@@ -273,12 +275,12 @@ NSResult NSSendTopicList(OCEntityHandlerRequest * entityHandlerRequest)
         OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_CONSUMER_ID, id);\r
     }\r
     OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, NSGetProviderInfo()->providerId);\r
+    OICFree(copyReq);\r
 \r
     if (topics)\r
     {\r
         NS_LOG(DEBUG, "topicList is NULL");\r
         size_t dimensionSize = (size_t) NSProviderGetTopicListSize(topics);\r
-\r
         NS_LOG_V(DEBUG, "dimensionSize = %d", (int)dimensionSize);\r
 \r
         if (!dimensionSize)\r
@@ -290,8 +292,7 @@ NSResult NSSendTopicList(OCEntityHandlerRequest * entityHandlerRequest)
                 sizeof(OCRepPayload *) * dimensionSize);\r
         NS_VERIFY_NOT_NULL(payloadTopicArray, NS_ERROR);\r
 \r
-        size_t dimensions[3] =\r
-        { dimensionSize, 0, 0 };\r
+        size_t dimensions[3] = { dimensionSize, 0, 0 };\r
 \r
         for (int i = 0; i < (int) dimensionSize; i++)\r
         {\r
@@ -305,7 +306,9 @@ NSResult NSSendTopicList(OCEntityHandlerRequest * entityHandlerRequest)
             OCRepPayloadSetPropInt(payloadTopicArray[i], NS_ATTRIBUTE_TOPIC_SELECTION,\r
                     (int) topics->state);\r
 \r
-            topics = topics->next;\r
+            NSTopicLL * next = topics->next;\r
+            OICFree(topics);\r
+            topics = next;\r
         }\r
 \r
         OCRepPayloadSetPropObjectArray(payload, NS_ATTRIBUTE_TOPIC_LIST,\r
@@ -313,15 +316,14 @@ NSResult NSSendTopicList(OCEntityHandlerRequest * entityHandlerRequest)
     }\r
     else\r
     {\r
-        size_t dimensions[3] =\r
-        { 0, 0, 0 };\r
+        size_t dimensions[3] = { 0, 0, 0 };\r
 \r
         OCRepPayloadSetPropObjectArrayAsOwner(payload, NS_ATTRIBUTE_TOPIC_LIST,\r
                 (OCRepPayload **) NULL, dimensions);\r
     }\r
 \r
-    char * reqInterface =\r
-            NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_INTERFACE);\r
+    copyReq = OICStrdup(entityHandlerRequest->query);\r
+    char * reqInterface = NSGetValueFromQuery(copyReq, NS_QUERY_INTERFACE);\r
 \r
     if (reqInterface && strcmp(reqInterface, NS_INTERFACE_BASELINE) == 0)\r
     {\r
@@ -329,6 +331,7 @@ NSResult NSSendTopicList(OCEntityHandlerRequest * entityHandlerRequest)
         OCResourcePayloadAddStringLL(&payload->interfaces, NS_INTERFACE_READ);\r
         OCResourcePayloadAddStringLL(&payload->types, NS_ROOT_TYPE);\r
     }\r
+    OICFree(copyReq);\r
 \r
     response.requestHandle = entityHandlerRequest->requestHandle;\r
     response.resourceHandle = entityHandlerRequest->resource;\r