From f0c792a3c34467e7719849d2e6b39d8c03bb1313 Mon Sep 17 00:00:00 2001 From: Jihun Ha Date: Wed, 8 Mar 2017 22:33:41 +0900 Subject: [PATCH] Fix a defect detected by static code analyzer A logic to check a failure of malloc is added. Change-Id: I1414673e0745fa11c4358e39ea6fedc9e26277ad Signed-off-by: Jihun Ha Reviewed-on: https://gerrit.iotivity.org/gerrit/17601 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- service/easy-setup/enrollee/src/resourcehandler.c | 50 ++++++++++++++++++----- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/service/easy-setup/enrollee/src/resourcehandler.c b/service/easy-setup/enrollee/src/resourcehandler.c index 8fc9ad8..cdf27ed 100755 --- a/service/easy-setup/enrollee/src/resourcehandler.c +++ b/service/easy-setup/enrollee/src/resourcehandler.c @@ -997,13 +997,28 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest) OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status); OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode); - size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest , 0, 0}; - int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest * sizeof(int64_t)); - for(int i = 0 ; i < g_ESEasySetupResource.numRequest ; ++i) + if(g_ESEasySetupResource.numRequest > 0) { - connectRequest[i] = g_ESEasySetupResource.connectRequest[i]; + size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0}; + int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest * sizeof(int64_t)); + if(!connectRequest) + { + OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload"); + return NULL; + } + + for(int i = 0 ; i < g_ESEasySetupResource.numRequest ; ++i) + { + connectRequest[i] = g_ESEasySetupResource.connectRequest[i]; + } + OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions); + } + else + { + OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0"); + size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0}; + OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions); } - OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions); if(gWriteUserdataCb) { @@ -1061,13 +1076,28 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest) OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status); OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode); - size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest , 0, 0}; - int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest * sizeof(int64_t)); - for(int i = 0 ; i < g_ESEasySetupResource.numRequest ; ++i) + if(g_ESEasySetupResource.numRequest > 0) + { + size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0}; + int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest * sizeof(int64_t)); + if(!connectRequest) + { + OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload"); + return NULL; + } + + for(int i = 0 ; i < g_ESEasySetupResource.numRequest ; ++i) + { + connectRequest[i] = g_ESEasySetupResource.connectRequest[i]; + } + OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions); + } + else { - connectRequest[i] = g_ESEasySetupResource.connectRequest[i]; + OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0"); + size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0}; + OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions); } - OCRepPayloadSetIntArray(repPayload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions); if(gWriteUserdataCb) { -- 2.7.4