diff options
author | Jihun Ha <jihun.ha@samsung.com> | 2017-03-08 22:33:41 +0900 |
---|---|---|
committer | Uze Choi <uzchoi@samsung.com> | 2017-03-09 14:15:52 +0000 |
commit | f0c792a3c34467e7719849d2e6b39d8c03bb1313 (patch) | |
tree | f399f0f0c048bd6805bff5c2bebdffa909f6eab8 | |
parent | 94bef05f80290f8fecf18cbcb75ac76f6d25e766 (diff) | |
download | iotivity-f0c792a3c34467e7719849d2e6b39d8c03bb1313.tar.gz iotivity-f0c792a3c34467e7719849d2e6b39d8c03bb1313.tar.bz2 iotivity-f0c792a3c34467e7719849d2e6b39d8c03bb1313.zip |
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 <jihun.ha@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/17601
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
-rwxr-xr-x | service/easy-setup/enrollee/src/resourcehandler.c | 50 |
1 files changed, 40 insertions, 10 deletions
diff --git a/service/easy-setup/enrollee/src/resourcehandler.c b/service/easy-setup/enrollee/src/resourcehandler.c index 8fc9ad8d6..cdf27ed63 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) { |