From d7b95d123e45531ac9bb975cc098b478c3022485 Mon Sep 17 00:00:00 2001 From: "hyuna0213.jo" Date: Thu, 21 Jul 2016 13:26:01 +0900 Subject: [PATCH] removed defects detected by static analysis tool for linux - initialize some variables - adjusted code style - modified unreached code Change-Id: I88a5a3d097c7d7c755c06a7b33508050bcb35591 Signed-off-by: hyuna0213.jo Reviewed-on: https://gerrit.iotivity.org/gerrit/8555 Tested-by: jenkins-iotivity Reviewed-by: Jon A. Cruz --- .../connectivity/common/src/camutex_pthreads.c | 4 +- .../connectivity/src/adapter_util/caadapterutils.c | 2 +- .../csdk/connectivity/src/ip_adapter/caipserver.c | 14 +++-- .../src/ip_adapter/linux/caipnwmonitor.c | 14 +++-- .../connectivity/src/tcp_adapter/catcpserver.c | 9 +-- .../csdk/connectivity/test/ca_api_unittest.cpp | 3 +- .../csdk/connectivity/test/cablocktransfertest.cpp | 69 +++++++++++++--------- 7 files changed, 66 insertions(+), 49 deletions(-) diff --git a/resource/csdk/connectivity/common/src/camutex_pthreads.c b/resource/csdk/connectivity/common/src/camutex_pthreads.c index ef344fd..db790ec 100644 --- a/resource/csdk/connectivity/common/src/camutex_pthreads.c +++ b/resource/csdk/connectivity/common/src/camutex_pthreads.c @@ -425,8 +425,8 @@ CAWaitResult_t ca_cond_wait_for(ca_cond cond, ca_mutex mutex, uint64_t microseco retVal = CA_WAIT_SUCCESS; } #else - int ret; - struct timespec abstime; + int ret = 0; + struct timespec abstime = { .tv_sec = 0 }; #ifdef __ANDROID__ if (camutex_cond_timedwait_relative) diff --git a/resource/csdk/connectivity/src/adapter_util/caadapterutils.c b/resource/csdk/connectivity/src/adapter_util/caadapterutils.c index 9ddc7e8..888885b 100644 --- a/resource/csdk/connectivity/src/adapter_util/caadapterutils.c +++ b/resource/csdk/connectivity/src/adapter_util/caadapterutils.c @@ -176,7 +176,7 @@ void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storag VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null"); VERIFY_NON_NULL_VOID(sockaddr, CA_ADAPTER_UTILS_TAG, "sockaddr is null"); - struct addrinfo *addrs; + struct addrinfo *addrs = NULL; struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM, .ai_flags = AI_NUMERICHOST }; diff --git a/resource/csdk/connectivity/src/ip_adapter/caipserver.c b/resource/csdk/connectivity/src/ip_adapter/caipserver.c index 360d449..8108328 100644 --- a/resource/csdk/connectivity/src/ip_adapter/caipserver.c +++ b/resource/csdk/connectivity/src/ip_adapter/caipserver.c @@ -485,9 +485,11 @@ static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags) { char recvBuffer[COAP_MAX_PDU_SIZE] = {0}; - size_t len; - int level, type, namelen; - struct sockaddr_storage srcAddr; + size_t len = 0; + int level = 0; + int type = 0; + int namelen = 0; + struct sockaddr_storage srcAddr = { .ss_family = 0 }; unsigned char *pktinfo = NULL; #if !defined(WSA_CMSG_DATA) struct cmsghdr *cmp = NULL; @@ -667,7 +669,7 @@ static CASocketFd_t CACreateSocket(int family, uint16_t *port, bool isMulticast) } #endif struct sockaddr_storage sa = { .ss_family = family }; - socklen_t socklen; + socklen_t socklen = 0; if (family == AF_INET6) { @@ -1207,10 +1209,10 @@ static void sendData(int fd, const CAEndpoint_t *endpoint, (void)cast; // eliminates release warning (void)fam; - struct sockaddr_storage sock; + struct sockaddr_storage sock = { .ss_family = 0 }; CAConvertNameToAddr(endpoint->addr, endpoint->port, &sock); - socklen_t socklen; + socklen_t socklen = 0; if (sock.ss_family == AF_INET6) { /** @todo figure out correct usage for ifindex, and sin6_scope_id */ diff --git a/resource/csdk/connectivity/src/ip_adapter/linux/caipnwmonitor.c b/resource/csdk/connectivity/src/ip_adapter/linux/caipnwmonitor.c index cf1a771..40a0552 100644 --- a/resource/csdk/connectivity/src/ip_adapter/linux/caipnwmonitor.c +++ b/resource/csdk/connectivity/src/ip_adapter/linux/caipnwmonitor.c @@ -218,11 +218,15 @@ CAInterface_t *CAFindInterfaceChange() { CAInterface_t *foundNewInterface = NULL; #ifdef __linux__ - char buf[4096]; - struct nlmsghdr *nh; - struct sockaddr_nl sa; - struct iovec iov = { buf, sizeof (buf) }; - struct msghdr msg = { (void *)&sa, sizeof (sa), &iov, 1, NULL, 0, 0 }; + char buf[4096] = { 0 }; + struct nlmsghdr *nh = NULL; + struct sockaddr_nl sa = { .nl_family = 0 }; + struct iovec iov = { .iov_base = buf, + .iov_len = sizeof (buf) }; + struct msghdr msg = { .msg_name = (void *)&sa, + .msg_namelen = sizeof (sa), + .msg_iov = &iov, + .msg_iovlen = 1 }; size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0); diff --git a/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c b/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c index 2300a4d..737c41d 100644 --- a/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c +++ b/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c @@ -50,11 +50,6 @@ #define TAG "OIC_CA_TCP_SERVER" /** - * Server port number for local test. - */ -#define SERVER_PORT 8000 - -/** * Maximum CoAP over TCP header length * to know the total data length. */ @@ -481,7 +476,7 @@ static int CATCPCreateSocket(int family, CATCPSessionInfo_t *svritem) } // #3. set socket length. - socklen_t socklen; + socklen_t socklen = 0; if (sa.ss_family == AF_INET6) { struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)&sa; @@ -519,7 +514,7 @@ static int CACreateAcceptSocket(int family, CASocket_t *sock) return sock->fd; } - socklen_t socklen; + socklen_t socklen = 0; struct sockaddr_storage server = { .ss_family = family }; int fd = socket(family, SOCK_STREAM, IPPROTO_TCP); diff --git a/resource/csdk/connectivity/test/ca_api_unittest.cpp b/resource/csdk/connectivity/test/ca_api_unittest.cpp index 6a94700..eccca11 100644 --- a/resource/csdk/connectivity/test/ca_api_unittest.cpp +++ b/resource/csdk/connectivity/test/ca_api_unittest.cpp @@ -29,7 +29,8 @@ class CATests : public testing::Test { protected: - virtual void SetUp() { + virtual void SetUp() + { CAInitialize(); } diff --git a/resource/csdk/connectivity/test/cablocktransfertest.cpp b/resource/csdk/connectivity/test/cablocktransfertest.cpp index 77cb32f..5efc456 100644 --- a/resource/csdk/connectivity/test/cablocktransfertest.cpp +++ b/resource/csdk/connectivity/test/cablocktransfertest.cpp @@ -213,14 +213,18 @@ TEST_F(CABlockTransferTests, CAGetPayloadFromBlockDataListTest) CABlockData_t *currData = CACreateNewBlockData(cadata); EXPECT_TRUE(currData != NULL); - size_t fullPayload = 0; - CAPayload_t payload = CAGetPayloadFromBlockDataList(currData->blockDataId, - &fullPayload); + if (currData) + { + size_t fullPayload = 0; + CAPayload_t payload = CAGetPayloadFromBlockDataList(currData->blockDataId, + &fullPayload); - size_t payloadLen = (payload != NULL) ? strlen((const char*) payload) : 0; - EXPECT_TRUE(fullPayload == payloadLen); + size_t payloadLen = (payload != NULL) ? strlen((const char*) payload) : 0; + EXPECT_TRUE(fullPayload == payloadLen); + + CARemoveBlockDataFromList(currData->blockDataId); + } - CARemoveBlockDataFromList(currData->blockDataId); CADestroyDataSet(cadata); coap_delete_list(options); coap_delete_pdu(pdu); @@ -306,12 +310,15 @@ TEST_F(CABlockTransferTests, CAAddBlockOption1InRequest) CABlockData_t *currData = CACreateNewBlockData(cadata); EXPECT_TRUE(currData != NULL); - EXPECT_EQ(CA_STATUS_OK, CAUpdateBlockOptionType(currData->blockDataId, - COAP_OPTION_BLOCK1)); + if (currData) + { + EXPECT_EQ(CA_STATUS_OK, CAUpdateBlockOptionType(currData->blockDataId, + COAP_OPTION_BLOCK1)); - EXPECT_EQ(CA_STATUS_OK, CAAddBlockOption1(&pdu, &requestData, - requestData.payloadSize, - currData->blockDataId, &options)); + EXPECT_EQ(CA_STATUS_OK, CAAddBlockOption1(&pdu, &requestData, + requestData.payloadSize, + currData->blockDataId, &options)); + } CADestroyDataSet(cadata); coap_delete_list(options); @@ -359,12 +366,15 @@ TEST_F(CABlockTransferTests, CAAddBlockOption1InResponse) CABlockData_t *currData = CACreateNewBlockData(cadata); EXPECT_TRUE(currData != NULL); - EXPECT_EQ(CA_STATUS_OK, CAUpdateBlockOptionType(currData->blockDataId, - COAP_OPTION_BLOCK1)); + if (currData) + { + EXPECT_EQ(CA_STATUS_OK, CAUpdateBlockOptionType(currData->blockDataId, + COAP_OPTION_BLOCK1)); - EXPECT_EQ(CA_STATUS_OK, CAAddBlockOption1(&pdu, &responseData, - responseData.payloadSize, - currData->blockDataId, &options)); + EXPECT_EQ(CA_STATUS_OK, CAAddBlockOption1(&pdu, &responseData, + responseData.payloadSize, + currData->blockDataId, &options)); + } CADestroyDataSet(cadata); coap_delete_list(options); @@ -412,12 +422,15 @@ TEST_F(CABlockTransferTests, CAAddBlockOption2InResponse) CABlockData_t *currData = CACreateNewBlockData(cadata); EXPECT_TRUE(currData != NULL); - EXPECT_EQ(CA_STATUS_OK, CAUpdateBlockOptionType(currData->blockDataId, - COAP_OPTION_BLOCK2)); + if (currData) + { + EXPECT_EQ(CA_STATUS_OK, CAUpdateBlockOptionType(currData->blockDataId, + COAP_OPTION_BLOCK2)); - EXPECT_EQ(CA_STATUS_OK, CAAddBlockOption2(&pdu, &responseData, - responseData.payloadSize, - currData->blockDataId, &options)); + EXPECT_EQ(CA_STATUS_OK, CAAddBlockOption2(&pdu, &responseData, + responseData.payloadSize, + currData->blockDataId, &options)); + } CADestroyDataSet(cadata); coap_delete_list(options); @@ -464,14 +477,16 @@ TEST_F(CABlockTransferTests, CAAddBlockOption2InRequest) CABlockData_t *currData = CACreateNewBlockData(cadata); EXPECT_TRUE(currData != NULL); - EXPECT_EQ(CA_STATUS_OK, CAUpdateBlockOptionType(currData->blockDataId, - COAP_OPTION_BLOCK2)); + if (currData) + { + EXPECT_EQ(CA_STATUS_OK, CAUpdateBlockOptionType(currData->blockDataId, + COAP_OPTION_BLOCK2)); - EXPECT_EQ(CA_STATUS_OK, CAAddBlockOption2(&pdu, &requestData, - requestData.payloadSize, - currData->blockDataId, &options)); + EXPECT_EQ(CA_STATUS_OK, CAAddBlockOption2(&pdu, &requestData, + requestData.payloadSize, + currData->blockDataId, &options)); + } - CARemoveBlockDataFromList(currData->blockDataId); CADestroyDataSet(cadata); coap_delete_list(options); coap_delete_pdu(pdu); -- 2.7.4