From 7544b239e4efa9f2cc6b05f1eee96444808089a1 Mon Sep 17 00:00:00 2001 From: Mandeep Shetty Date: Tue, 16 Jun 2015 10:11:13 -0700 Subject: [PATCH] Fix segfault on GET for device and platform info. 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 Reviewed-on: https://gerrit.iotivity.org/gerrit/1285 Tested-by: jenkins-iotivity Reviewed-by: Sakthivel Samidurai Reviewed-by: Erich Keane --- resource/csdk/stack/src/ocresource.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/resource/csdk/stack/src/ocresource.c b/resource/csdk/stack/src/ocresource.c index 2314461..2694ced 100644 --- a/resource/csdk/stack/src/ocresource.c +++ b/resource/csdk/stack/src/ocresource.c @@ -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)); -- 2.7.4