Fix latent error for OCCreateResourceWithHost.
authorHyunJun Kim <hyunjun2.kim@samsung.com>
Thu, 7 May 2015 07:26:12 +0000 (16:26 +0900)
committerUze Choi <uzchoi@samsung.com>
Thu, 7 May 2015 09:17:35 +0000 (09:17 +0000)
It was crashed when OCCreateResource calls using invalid uri or
already used uri.
OCCreateResource can return OC_STACK_OK, OC_STACK_INVALID_PARAM,
OC_STACK_INVALID_URI and so on.

Currently, it checks only if the result is OC_STACK_ERROR.
But, there can be a case where the result is not OC_STACK_ERROR
but 'handle' is NULL. In this case with this conditional statement,
some exceptional errors could happen.

Therefore, it should be changed for the conditional statement to check
if the resource creation is successful as intended.

Change-Id: I9921b3bb7874b1dbf8b0fda5483471506da93882
Signed-off-by: HyunJun Kim <hyunjun2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/923
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
resource/csdk/stack/src/ocstack.c

index f709623..08793a3 100644 (file)
@@ -2574,11 +2574,13 @@ OCStackResult OCCreateResourceWithHost(OCResourceHandle *handle,
         OCEntityHandler entityHandler,
         uint8_t resourceProperties)
 {
+    OC_LOG(INFO, TAG, PCF("Entering OCCreateResourceWithHost"));
     char *str = NULL;
     size_t size = 0;
 
     if(!host)
     {
+        OC_LOG(ERROR, TAG, PCF("Added resource host is NULL."));
         return OC_STACK_INVALID_PARAM;
     }
 
@@ -2587,16 +2589,18 @@ OCStackResult OCCreateResourceWithHost(OCResourceHandle *handle,
     result = OCCreateResource(handle, resourceTypeName, resourceInterfaceName,
                                 uri, entityHandler, resourceProperties);
 
-    if (result != OC_STACK_ERROR)
+    if (result == OC_STACK_OK)
     {
         // Set the uri
         size = strlen(host) + 1;
         str = (char *) OCMalloc(size);
         if (!str)
         {
+            OC_LOG(ERROR, TAG, PCF("Memory could not be allocated."));
             return OC_STACK_NO_MEMORY;
         }
         strncpy(str, host, size);
+
         ((OCResource *) *handle)->host = str;
     }