Fix segfault on GET for device and platform info.
authorMandeep Shetty <mandeep.shetty@intel.com>
Tue, 16 Jun 2015 17:11:13 +0000 (10:11 -0700)
committerErich Keane <erich.keane@intel.com>
Wed, 17 Jun 2015 15:48:57 +0000 (15:48 +0000)
The mandatory fields for device and platform are not enforced by the
stack on init. They are enforced only when the info is explicitly added.
Servers that did not register this device and platform info segfaulted
when getting discovery requests from clients.
Added a null check before dereferencing a pointer to fix the problem.

Change-Id: I8b77d149014329e60f8aa8e9c1efb761394487da
Signed-off-by: Mandeep Shetty <mandeep.shetty@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1285
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Sakthivel Samidurai <sakthivel.samidurai@intel.com>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/csdk/stack/src/ocresource.c

index 2314461..2694ced 100644 (file)
@@ -121,8 +121,22 @@ static char* GetJSONStringFromPlatformInfo(OCPlatformInfo info)
 
     cJSON_AddItemToObject (rootObj, OC_RSRVD_REPRESENTATION, repObj = cJSON_CreateObject());
 
-    cJSON_AddItemToObject (repObj, OC_RSRVD_PLATFORM_ID, cJSON_CreateString(info.platformID));
-    cJSON_AddItemToObject (repObj, OC_RSRVD_MFG_NAME, cJSON_CreateString(info.manufacturerName));
+    if (!repObj)
+    {
+        return NULL;
+    }
+    if (info.platformID)
+    {
+        cJSON_AddItemToObject (repObj, OC_RSRVD_PLATFORM_ID,
+                cJSON_CreateString(info.platformID));
+    }
+
+    if (info.manufacturerName)
+    {
+        cJSON_AddItemToObject (repObj, OC_RSRVD_MFG_NAME,
+                cJSON_CreateString(info.manufacturerName));
+    }
+
     if (info.manufacturerUrl)
     {
         cJSON_AddItemToObject (repObj, OC_RSRVD_MFG_URL,
@@ -201,11 +215,19 @@ static char* GetJSONStringFromDeviceInfo(OCDeviceInfo info)
 
     cJSON_AddItemToObject (rootObj, OC_RSRVD_REPRESENTATION, repObj = cJSON_CreateObject());
 
+    if (!repObj)
+    {
+        return NULL;
+    }
+
     cJSON_AddItemToObject (repObj, OC_RSRVD_DEVICE_ID,
                     cJSON_CreateString(OCGetServerInstanceIDString()));
 
-    cJSON_AddItemToObject (repObj, OC_RSRVD_DEVICE_NAME,
+    if (info.deviceName)
+    {
+        cJSON_AddItemToObject (repObj, OC_RSRVD_DEVICE_NAME,
                         cJSON_CreateString(info.deviceName));
+    }
 
     cJSON_AddItemToObject (repObj, OC_RSRVD_SPEC_VERSION,
                         cJSON_CreateString(OC_SPEC_VERSION));