code cleanup.
authorjaesick.shin <jaesick.shin@samsung.com>
Mon, 25 Jul 2016 07:25:30 +0000 (16:25 +0900)
committerUze Choi <uzchoi@samsung.com>
Tue, 26 Jul 2016 00:39:38 +0000 (00:39 +0000)
This commit included code cleanup.
1. coding idiom
2. double free.
3. warning.
4. unlock

second patch included
change the mutex_unlock in NSProviderInterface.

Change-Id: Iff615c577f0cb0e868fa29c1592c523c9bed4d6f
Signed-off-by: jaesick.shin <jaesick.shin@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/9679
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/notification/examples/linux/notificationprovider.c
service/notification/include/NSCommon.h
service/notification/src/common/NSUtil.c
service/notification/src/provider/NSProviderInterface.c
service/notification/src/provider/NSProviderNotification.c
service/notification/src/provider/NSProviderResource.c
service/notification/src/provider/NSProviderScheduler.c
service/notification/src/provider/NSProviderSubscription.c
service/notification/src/provider/NSProviderSystem.c
service/notification/src/provider/cache/linux/NSProviderMemoryCache.c
service/notification/src/provider/cache/linux/NSProviderMemoryCache.h

index 6b0cddeaf141d367c2e3400b77bebed49d238b4f..aad18bf05849f5aa643759c6d8a6520fd6bf1f9d 100644 (file)
@@ -190,11 +190,6 @@ int main()
 \r
             case 4:\r
                 printf("NSRead");\r
-                NSSyncInfo * sync = (NSSyncInfo*) OICMalloc(sizeof(NSSyncInfo));\r
-\r
-                sync->messageId = OICStrdup("dev_001");\r
-                sync->state = 1;\r
-\r
                 break;\r
 \r
             case 5:\r
@@ -210,7 +205,6 @@ int main()
                 printf("Session Code: ");\r
                 gets(REMOTE_SERVER_SESSION);\r
 \r
-\r
                 NSCloudLogin(REMOTE_SERVER_ADDRESS, REMOTE_SERVER_SESSION, CloudLoginoutCallback);\r
                 printf("OCCloudLogin requested");\r
                 break;\r
index e11fa79a5fcec26be008e614d32ac7e67ae254b4..41beb5d42963b7de4de57a76da1aeb0b989d7fc4 100644 (file)
@@ -44,8 +44,6 @@
 #define NS_ATTRIBUTE_DATETIME "DATE_TIME"
 #define NS_ATTRIBUTE_TTL "TTL"
 
-#include <stdint.h>
-
 /**
  * Result code of notification service
  */
index 5b6f41be94e8c1e2c56a99313a54df79d8a458be..541cdd837f911f0b236d5e661d0d0406a27055bb 100755 (executable)
@@ -163,7 +163,7 @@ NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
 {
     NSSyncInfo * newMsg = NULL;
 
-    if(copyMsg == NULL)
+    if(!copyMsg)
     {
         NS_LOG(ERROR, "Copy Msg is NULL");
         return NULL;
@@ -171,6 +171,12 @@ NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
 
     newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
 
+    if(!newMsg)
+    {
+        NS_LOG(ERROR, "newMsg is NULL");
+        return NULL;
+    }
+
     newMsg->messageId = copyMsg->messageId;
     OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
     newMsg->state = copyMsg->state;
@@ -204,6 +210,13 @@ NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
     }
 
     newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
+
+    if(!newMsg)
+    {
+        NS_LOG(ERROR, "newMsg is NULL");
+        return NULL;
+    }
+
     (newMsg->consumerId)[0] = '\0';
 
     OICStrcpy(newMsg->consumerId, UUID_STRING_SIZE, copyMsg->consumerId);
@@ -233,7 +246,7 @@ NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
 {
     NS_LOG(DEBUG, "NSGetSyncInfo - IN");
     char * providerId = NULL;
-    int64_t state;
+    int64_t state = 0;
 
     if(!payload)
     {
@@ -362,6 +375,12 @@ NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj)
 
     NSMediaContents * newObj = (NSMediaContents *)OICMalloc(sizeof(NSMediaContents));
 
+    if(!newObj)
+    {
+        NS_LOG(ERROR, "contents newObj is NULL");
+        return NULL;
+    }
+
     if(copyObj->iconImage)
     {
         newObj->iconImage = OICStrdup(copyObj->iconImage);
@@ -374,7 +393,7 @@ NSResult NSFreeMediaContents(NSMediaContents * obj)
 {
     if(!obj)
     {
-        return NS_OK;
+        return NS_FAIL;
     }
 
     NSFreeMalloc(&(obj->iconImage));
@@ -386,6 +405,13 @@ NSResult NSFreeMediaContents(NSMediaContents * obj)
 NSMessage * NSInitializeMessage()
 {
     NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));
+
+    if(!msg)
+    {
+        NS_LOG(ERROR, "Msg is NULL");
+        return NULL;
+    }
+
     msg->messageId = OICGetCurrentTime(TIME_IN_MS);
     (msg->providerId)[0] = '\0';
     msg->type = 0;
index f60ef7bd32dc164b90a989be8f91f87adbf660ad..e2c7061fd54dcdc215911e94977a97afe39dde30 100644 (file)
@@ -119,6 +119,7 @@ NSResult NSProviderEnableRemoteService(char *serverAddress)
     if(!initProvider)\r
     {\r
         NS_LOG(DEBUG, "Provider service has not been started yet");\r
+        pthread_mutex_unlock(&nsInitMutex);\r
         return NS_FAIL;\r
     }\r
 \r
index 339357fdc62bbc21fb580a7a1d64679c8b3b3c12..a149efdcdcc1806ed3d262a0cd9812b4db8ffdf0 100644 (file)
@@ -100,7 +100,7 @@ NSResult NSSendNotification(NSMessage *msg)
         return NS_ERROR;
     }
 
-    OCRepPayload* payload;
+    OCRepPayload* payload = NULL;
 
     if (NSSetMessagePayload(msg, &payload) != NS_OK)
     {
@@ -123,10 +123,14 @@ NSResult NSSendNotification(NSMessage *msg)
         if (subData->isWhite)
         {
             if(subData->messageObId != 0)
+            {
                 obArray[obCount++] = subData->messageObId;
+            }
 
             if(subData->remote_messageObId != 0)
+            {
                 obArray[obCount++] = subData->remote_messageObId;
+            }
         }
         it = it->next;
     }
@@ -193,10 +197,14 @@ NSResult NSSendSync(NSSyncInfo *sync)
         if (subData->isWhite)
         {
             if(subData->syncObId != 0)
+            {
                 obArray[obCount++] = subData->syncObId;
+            }
 
             if(subData->remote_syncObId != 0)
+            {
                 obArray[obCount++] = subData->remote_syncObId;
+            }
         }
         it = it->next;
     }
index 8da879673aed7d8e75889528871e851b61d35e22..a0fa445524fb3258171f2032d5f201eb626b1234 100644 (file)
@@ -19,6 +19,7 @@
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 #include "NSProviderResource.h"
+#include <string.h>
 
 NSNotificationResource NotificationResource;
 NSMessageResource NotificationMessageResource;
@@ -27,6 +28,8 @@ NSSyncResource NotificationSyncResource;
 OCStackApplicationResult NSHandlePublishCb(void *ctx, OCDoHandle handle,
     OCClientResponse *clientResponse)
 {
+    (void) handle;
+
     if (ctx != (void *)DEFAULT_CONTEXT_VALUE)
     {
         NS_LOG(DEBUG, "Invalid publish callback received");
index 64032be3a6fa693409898a8c770ee6afc1aefded..a576fa9f17b724b87f5ce1fdda989b0eaab688b4 100755 (executable)
@@ -143,8 +143,10 @@ bool NSStopScheduler()
 void NSPushQueue(NSSchedulerType schedulerType, NSTaskType taskType, void* data)\r
 {\r
 \r
-    if(NSIsRunning[schedulerType] == false)\r
+    if(!NSIsRunning[schedulerType])\r
+    {\r
         return;\r
+    }\r
 \r
     pthread_mutex_lock(&NSMutex[schedulerType]);\r
 \r
@@ -155,7 +157,6 @@ void NSPushQueue(NSSchedulerType schedulerType, NSTaskType taskType, void* data)
     if (NSHeadMsg[schedulerType] == NULL)\r
     {\r
         NSHeadMsg[schedulerType] = (NSTask*) OICMalloc(sizeof(NSTask));\r
-        memset(NSHeadMsg[schedulerType], 0, sizeof(NSTask));\r
         NSHeadMsg[schedulerType]->taskType = taskType;\r
         NSHeadMsg[schedulerType]->taskData = data;\r
         NSHeadMsg[schedulerType]->nextTask = NULL;\r
@@ -164,7 +165,6 @@ void NSPushQueue(NSSchedulerType schedulerType, NSTaskType taskType, void* data)
     else\r
     {\r
         NSTask* newNode = (NSTask*) OICMalloc(sizeof(NSTask));\r
-        memset(newNode, 0, sizeof(NSTask));\r
         newNode->taskType = taskType;\r
         newNode->taskData = data;\r
         newNode->nextTask = NULL;\r
index bb8d74298339d4738d52ad72e85aa300aaf91022..3c2c267fb429b561c5b71a4014a44babc53c4eec 100644 (file)
@@ -60,7 +60,7 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest
     NS_LOG(DEBUG, "NSSendAccessPolicyResponse - IN");\r
 \r
     // put notification resource\r
-    OCResourceHandle notificationResourceHandle;\r
+    OCResourceHandle notificationResourceHandle = NULL;\r
     if (NSPutNotificationResource(NSGetSubscriptionAccepter(), &notificationResourceHandle)\r
             != NS_OK)\r
     {\r
@@ -244,7 +244,7 @@ NSResult NSSendResponse(const char * id, bool accepted)
         return NS_ERROR;\r
     }\r
 \r
-    OCResourceHandle rHandle;\r
+    OCResourceHandle rHandle = NULL;\r
     if (NSPutMessageResource(NULL, &rHandle) != NS_OK)\r
     {\r
         NS_LOG(ERROR, "Fail to put notification resource");\r
index 2a6c6ee232b133b24c7093884515c83aee3f37d3..b9b2bf6f2ffe7acb1ca47a7e96bdec4cf944d186 100644 (file)
@@ -71,7 +71,7 @@ void NSInitProviderInfo()
     providerInfo = (NSProviderInfo *) OICMalloc(sizeof(NSProviderInfo));\r
     const char * generatedUuid = (char *)OCGetServerInstanceIDString();\r
     NS_LOG_V(DEBUG, "Generate Provider ID: %s", generatedUuid);\r
-    OICStrcpy(providerInfo->providerId, strlen(generatedUuid), generatedUuid);\r
+    OICStrcpy(providerInfo->providerId, UUID_STRING_SIZE, generatedUuid);\r
 \r
     providerInfo->providerName = NULL;\r
 }\r
index bf761e56174f0c2287acdf85292a63bc35fe4b75..66d88f90452c08b2e57da0da161545b79b3f99f7 100755 (executable)
@@ -19,6 +19,7 @@
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
 \r
 #include "NSProviderMemoryCache.h"\r
+#include <string.h>\r
 \r
 NSCacheList * NSStorageCreate()\r
 {\r
@@ -204,7 +205,9 @@ NSResult NSStorageWrite(NSCacheList * list, NSCacheElement * newObj)
 \r
         NSCacheMsgData * msgData = (NSCacheMsgData *) newObj->data;\r
 \r
+        pthread_mutex_unlock(&NSCacheMutex);\r
         NSCacheElement * it = NSStorageRead(list, msgData->id);\r
+        pthread_mutex_lock(&NSCacheMutex);\r
         if (it)\r
         {\r
             NSCacheMsgData * itData = (NSCacheMsgData *) it->data;\r
@@ -248,7 +251,9 @@ NSResult NSStorageDelete(NSCacheList * list, const char * delId)
         if (del == list->head) // first object\r
         {\r
             if (del == list->tail) // first object (one object)\r
+            {\r
                 list->tail = del->next;\r
+            }\r
 \r
             list->head = del->next;\r
 \r
@@ -265,7 +270,9 @@ NSResult NSStorageDelete(NSCacheList * list, const char * delId)
         if (NSProviderCompareIdCacheData(type, del->data, delId))\r
         {\r
             if (del == list->tail) // delete object same to last object\r
+            {\r
                 list->tail = prev;\r
+            }\r
 \r
             prev->next = del->next;\r
             NSProviderDeleteCacheData(type, del->data);\r
@@ -391,7 +398,10 @@ bool NSIsSameObId(NSCacheSubData * data, OCObservationId id)
 {\r
     if (id == data->messageObId || id == data->syncObId || id == data->remote_messageObId ||\r
                 id == data->remote_syncObId)\r
+    {\r
         return true;\r
+    }\r
+\r
     return false;\r
 }\r
 \r
@@ -416,7 +426,9 @@ NSResult NSProviderDeleteSubDataFromObId(NSCacheList * list, OCObservationId id)
             if (del == list->head) // first object\r
             {\r
                 if (del == list->tail) // first object (one object)\r
+                {\r
                     list->tail = del->next;\r
+                }\r
 \r
                 list->head = del->next;\r
 \r
@@ -433,7 +445,9 @@ NSResult NSProviderDeleteSubDataFromObId(NSCacheList * list, OCObservationId id)
                 if (NSIsSameObId(curr, id))\r
                 {\r
                     if (del == list->tail) // delete object same to last object\r
+                    {\r
                         list->tail = prev;\r
+                    }\r
 \r
                     prev->next = del->next;\r
                     NSProviderDeleteCacheData(type, del->data);\r
index 218ce9241ebbab61eb70f06009d860e1e8eeddbe..43cca9c965cd4f4f37528b5191d694ed61b7b4c2 100755 (executable)
@@ -23,7 +23,6 @@
 \r
 #include <pthread.h>\r
 #include <stdbool.h>\r
-#include <string.h>\r
 \r
 #include "NSCommon.h"\r
 #include "NSConstants.h"\r