Fix for result of static analysis. 47/14947/6
authorKIM JungYong <jyong2.kim@samsung.com>
Wed, 30 Nov 2016 06:08:29 +0000 (15:08 +0900)
committerUze Choi <uzchoi@samsung.com>
Mon, 5 Dec 2016 06:36:20 +0000 (06:36 +0000)
In this patch, incorrect coding rule is fixed and resolved warning of static analysis.
1. Uninitialized variables is fixed with initialized.
2. Invalid bracket location is fixed.
3. Incorrect division with zero is prevented.
4. Incorrect referenced pointer is prevented.

Change-Id: I715d671241f3d48a1174c49cbe34074b31a50f02
Signed-off-by: KIM JungYong <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/14947
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
20 files changed:
service/resource-container/examples/BMISensorBundle/src/BMISensor.cpp
service/resource-container/examples/BMISensorBundle/src/inputSensors/HeightSensorApp/src/HeightSensorApp.cpp
service/resource-container/examples/BMISensorBundle/src/inputSensors/WeightSensorApp/src/WeightSensorApp.cpp
service/resource-container/examples/ContainerSample.cpp
service/resource-container/examples/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensor.cpp
service/resource-container/examples/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensorResource.cpp
service/resource-container/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp/src/ThingResourceServer.cpp
service/resource-container/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp1/src/ThingResourceServer1.cpp
service/resource-container/examples/HueSampleBundle/src/HueConnector.cpp
service/resource-container/src/Configuration.cpp
service/resource-container/src/ResourceContainerImpl.cpp
service/resource-container/unittests/ResourceContainerTest.cpp
service/resource-encapsulation/examples/linux/NestedAttributesClient.cpp
service/resource-encapsulation/examples/linux/NestedAttributesServer.cpp
service/resource-encapsulation/examples/linux/SampleResourceClient.cpp
service/resource-encapsulation/examples/linux/SampleResourceServer.cpp
service/resource-encapsulation/examples/linux/SeparateResponseServer.cpp
service/resource-encapsulation/src/common/expiryTimer/unittests/ExpiryTimerTest.cpp
service/resource-encapsulation/src/resourceBroker/unittest/ResourceBrokerUnitTest.cpp
service/resource-encapsulation/src/resourceBroker/unittest/ResourcePresenceUnitTest.cpp

index 6bf73e1..53e96cf 100644 (file)
@@ -52,7 +52,7 @@ BMISensor::~BMISensor()
 int BMISensor::executeBMISensorLogic(std::map<std::string, std::string> *pInputData,
                                      std::string *pOutput)
 {
-    BMIResult result;
+    BMIResult result = ERROR;
 
     if (pInputData->find("weight") != pInputData->end())
     {
@@ -81,10 +81,12 @@ int BMISensor::executeBMISensorLogic(std::map<std::string, std::string> *pInputD
  */
 BMIResult BMISensor::makeBMI(void)
 {
-    double BMIvalue, timediffsecond;
-    double dWeight, dHeight;
+    double BMIvalue = 0.0;
+    double timediffsecond = 0.0;
+    double dWeight = 0.0;
+    double dHeight = 0.0;
 
-    int BMIResult;
+    int BMIResult = 0;
 
     if (!m_weight.empty() && !m_height.empty())
     {
@@ -147,4 +149,4 @@ BMIResult BMISensor::makeBMI(void)
     }
 
     return ERROR;
-}
\ No newline at end of file
+}
index 9d43288..2a9dd86 100644 (file)
@@ -52,7 +52,7 @@ OCResourceHandle HeightResource::getHandle()
 
 void HeightResource::setResourceRepresentation(OCRepresentation &rep)
 {
-    double tempHeight;
+    double tempHeight = 0.0;
 
     rep.getValue("height", tempHeight);
 
@@ -80,7 +80,7 @@ void *TestSensorVal(void *param)
 
     bool bFlag = true;
     int nSleep_time = INTERVAL_FOR_CHECK;
-    double nHeight;
+    double nHeight = 0.0;
 
     std::cout << "[HeightSensorAPP] ::" << __func__ << " is called."
               << std::endl;
@@ -179,7 +179,7 @@ OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request
 
         if (requestFlag & RequestHandlerFlag::ObserverFlag)
         {
-            pthread_t threadId;
+            pthread_t threadId = {};
 
             cout << "\t\trequestFlag : Observer\n";
             g_Observation = 1;
@@ -220,7 +220,7 @@ int main()
 
         OC::OCPlatform::stopPresence();
     }
-    catch (std::exception e)
+    catch (std::exception e)
     {
         cout << e.what() << endl;
     }
index 07b8096..a52cb70 100644 (file)
@@ -53,7 +53,7 @@ OCResourceHandle WeightResource::getHandle()
 
 void WeightResource::setResourceRepresentation(OCRepresentation &rep)
 {
-    double tempWeight;
+    double tempWeight = 0.0;
 
     rep.getValue("weight", tempWeight);
 
@@ -80,7 +80,7 @@ void *TestSensorVal(void *param)
 
     bool bFlag = true;
     int nSleep_time = INTERVAL_FOR_CHECK;
-    double nWeight;
+    double nWeight = 0.0;
 
     std::cout << "[WeightSensorAPP] ::" << __func__ << " is called."
               << std::endl;
@@ -180,7 +180,7 @@ OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request
 
         if (requestFlag & RequestHandlerFlag::ObserverFlag)
         {
-            pthread_t threadId;
+            pthread_t threadId = {};
 
             cout << "\t\trequestFlag : Observer\n";
             g_Observation = 1;
index 4d1c2df..32397b7 100644 (file)
@@ -68,7 +68,7 @@ RCSResourceContainer *g_pResourceContainer = nullptr;
 
 void getCurrentPath(std::string *pPath)
 {
-    char buffer[MAX_PATH];
+    char buffer[MAX_PATH] = {0,};
 
     if (!pPath->empty())
     {
@@ -95,7 +95,7 @@ void getCurrentPath(std::string *pPath)
 
 int processUserInput(int min, int max)
 {
-    int input;
+    int input = 0;
 
     std::cin >> input;
 
index 5da9ebb..7fd0309 100644 (file)
@@ -51,7 +51,7 @@ int DiscomfortIndexSensor::executeDISensorLogic(std::map<std::string, std::strin
     std::cout << "[DiscomfortIndexSensor] DiscomfortIndexSensor::" << __func__ << " is called."
               << std::endl;
 
-    DIResult result;
+    DIResult result = ERROR;
 
     m_temperature = pInputData->at("temperature");
     m_humidity = pInputData->at("humidity");
@@ -116,4 +116,4 @@ DIResult DiscomfortIndexSensor::makeDiscomfortIndex()
     m_discomfortIndex = std::to_string(DILevel);
 
     return SUCCESS;
-}
\ No newline at end of file
+}
index 2f6f857..e98ce5f 100644 (file)
@@ -66,8 +66,8 @@ void DiscomfortIndexSensorResource::executeLogic()
 void DiscomfortIndexSensorResource::onUpdatedInputResource(const std::string attributeName,
         std::vector<RCSResourceAttributes::Value> values)
 {
-    double sum = 0;
-    double dConvert;
+    double sum = 0.0;
+    double dConvert = 0.0;
     int inputCount = 0;
     std::string itString;
 
@@ -80,7 +80,11 @@ void DiscomfortIndexSensorResource::onUpdatedInputResource(const std::string att
         ++inputCount;
     }
 
-    double result = sum / inputCount;
+    double result = 0.0;
+    if (inputCount)
+    {
+        result = sum / inputCount;
+    }
     std::string indexCount;//string which will contain the indexCount
     std::stringstream convert; // stringstream used for the conversion
     convert << result;//add the value of Number to the characters in the stream
index 0a11876..ed99f57 100644 (file)
@@ -59,8 +59,8 @@ OCResourceHandle TemphumidResource::getHandle()
 
 void TemphumidResource::setResourceRepresentation(OCRepresentation &rep)
 {
-    int tempHumid;
-    int tempTemp;
+    int tempHumid = 0;
+    int tempTemp = 0;
 
     rep.getValue("humidity", tempTemp);
     rep.getValue("temperature", tempHumid);
@@ -171,7 +171,7 @@ OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request
 
         if (requestFlag & RequestHandlerFlag::ObserverFlag)
         {
-            pthread_t threadId;
+            pthread_t threadId = {};
 
             cout << "\t\trequestFlag : Observer\n";
             g_Observation = 1;
@@ -212,7 +212,7 @@ int main()
 
         OC::OCPlatform::stopPresence();
     }
-    catch (std::exception e)
+    catch (std::exception e)
     {
         cout << e.what() << endl;
     }
index fdcba11..a1c9f07 100644 (file)
@@ -60,8 +60,8 @@ OCResourceHandle TemphumidResource::getHandle()
 
 void TemphumidResource::setResourceRepresentation(OCRepresentation &rep)
 {
-    int tempHumid;
-    int tempTemp;
+    int tempHumid = 0;
+    int tempTemp = 0;
 
     rep.getValue("humidity", tempTemp);
     rep.getValue("temperature", tempHumid);
@@ -170,7 +170,7 @@ OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request
 
         if (requestFlag & RequestHandlerFlag::ObserverFlag)
         {
-            pthread_t threadId;
+            pthread_t threadId = {};
 
             cout << "\t\trequestFlag : Observer\n";
             g_Observation = 1;
@@ -211,7 +211,7 @@ int main()
 
         OC::OCPlatform::stopPresence();
     }
-    catch (std::exception e)
+    catch (std::exception e)
     {
         cout << e.what() << endl;
     }
index de7121b..297faa1 100644 (file)
@@ -68,8 +68,8 @@ void HueConnector::disconnect()
 std::string HueConnector::transmit(std::string target, std::string payload)
 {
     std::cout << "Transmitting to " << target << " " << payload << endl;
-    CURL *curl;
-    CURLcode res;
+    CURL *curl = NULL;
+    CURLcode res = CURLE_OK;
     struct curl_slist *headers = NULL; /* http headers to send with request */
     /* set content type */
     headers = curl_slist_append(headers, "Accept: application/json");
@@ -114,8 +114,8 @@ static int writer(char *data, size_t size, size_t nmemb, std::string *buffer_in)
 std::string HueConnector::read(std::string target)
 {
     std::cout << "Reading from to " << target << endl;
-    CURL *curl;
-    CURLcode res;
+    CURL *curl = NULL;
+    CURLcode res = CURLE_OK;
     struct curl_slist *headers = NULL; /* http headers to send with request */
     /* set content type */
     headers = curl_slist_append(headers, "Accept: application/json");
index fc5e1ae..3a7fd95 100644 (file)
@@ -87,8 +87,8 @@ namespace OIC
 
         void Configuration::getConfiguredBundles(configInfo *configOutput)
         {
-            rapidxml::xml_node< char > *bundle;
-            rapidxml::xml_node< char > *subItem;
+            rapidxml::xml_node< char > *bundle = nullptr;
+            rapidxml::xml_node< char > *subItem = nullptr;
 
             string strKey, strValue;
 
@@ -130,7 +130,7 @@ namespace OIC
 
         void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
         {
-            rapidxml::xml_node< char > *bundle;
+            rapidxml::xml_node< char > *bundle = nullptr;
 
             string strBundleId, strPath, strVersion;
 
@@ -202,9 +202,11 @@ namespace OIC
         void Configuration::getResourceConfiguration(std::string bundleId, std::string resourceUri,
                         resourceInfo *resourceInfoOut)
         {
-            rapidxml::xml_node< char > *bundle;
-            rapidxml::xml_node< char > *resource;
-            rapidxml::xml_node< char > *item, *subItem, *subItem2;
+            rapidxml::xml_node< char > *bundle = nullptr;
+            rapidxml::xml_node< char > *resource = nullptr;
+            rapidxml::xml_node< char > *item = nullptr;
+            rapidxml::xml_node< char > *subItem = nullptr;
+            rapidxml::xml_node< char > *subItem2 = nullptr;
 
             string strBundleId;
             string strKey, strValue;
@@ -327,9 +329,11 @@ namespace OIC
         void Configuration::getResourceConfiguration(std::string bundleId,
                 std::vector< resourceInfo > *configOutput)
         {
-            rapidxml::xml_node< char > *bundle;
-            rapidxml::xml_node< char > *resource;
-            rapidxml::xml_node< char > *item, *subItem, *subItem2;
+            rapidxml::xml_node< char > *bundle = nullptr;
+            rapidxml::xml_node< char > *resource = nullptr;
+            rapidxml::xml_node< char > *item = nullptr;
+            rapidxml::xml_node< char > *subItem = nullptr;
+            rapidxml::xml_node< char > *subItem2 = nullptr;
 
             string strBundleId;
             string strKey, strValue;
@@ -347,7 +351,10 @@ namespace OIC
                                  bundle->next_sibling())
                         {
                             // <id>
-                            strBundleId = bundle->first_node(BUNDLE_ID)->value();
+                            if (bundle->first_node(BUNDLE_ID))
+                            {
+                                strBundleId = bundle->first_node(BUNDLE_ID)->value();
+                            }
 
                             OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle ids %s - %s !",
                                     strBundleId.c_str(), bundleId.c_str());
index e22692d..099acd5 100644 (file)
@@ -270,7 +270,7 @@ namespace OIC
             OIC_LOG_V(INFO, CONTAINER_TAG, "Unregister bundle: (%s)",
                      std::string(m_bundles[id]->getID()).c_str());
 
-            const char *error;
+            const char *error = NULL;
             dlclose(bundleHandle);
 
             if ((error = dlerror()) != NULL)
@@ -690,7 +690,7 @@ namespace OIC
         void ResourceContainerImpl::registerSoBundle(shared_ptr<RCSBundleInfo> bundleInfo)
         {
             OIC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle");
-            const char *error;
+            const char *error = NULL;
 
             activator_t *bundleActivator = NULL;
             deactivator_t *bundleDeactivator = NULL;
@@ -943,7 +943,7 @@ namespace OIC
         void ResourceContainerImpl::addSoBundleResource(const std::string &bundleId,
                 resourceInfo newResourceInfo)
         {
-            resourceCreator_t *resourceCreator;
+            resourceCreator_t *resourceCreator = nullptr;
 
             resourceCreator = m_bundles[bundleId]->getResourceCreator();
 
index 94415c3..13436b5 100644 (file)
@@ -57,7 +57,7 @@ string CONFIG_FILE = "ResourceContainerTestConfig.xml";
 
 void getCurrentPath(std::string *pPath)
 {
-    char buffer[MAX_PATH];
+    char buffer[MAX_PATH] = {0,};
 
 #if defined(__linux__)
     char *strPath = NULL;
index 4a7d87d..6ef9202 100644 (file)
@@ -183,7 +183,7 @@ std::vector< std::vector< RCSResourceAttributes > > createNestedAttribute(int sp
 
 int processUserInput()
 {
-    int userInput;
+    int userInput = 0;
     std::cin >> userInput;
     if (std::cin.fail())
     {
@@ -201,7 +201,8 @@ void getAttributeFromRemoteServer()
 
 void setAttributeToRemoteServer()
 {
-    int speed, airc;
+    int speed = 0;
+    int airc = 0;
 
     std::cout << "\tEnter the Fan Speed you want to set : ";
     std::cin >> speed;
index 66534f6..b85ea15 100644 (file)
@@ -223,7 +223,7 @@ void initServer()
 
 int processUserInput()
 {
-    int userInput;
+    int userInput = 0;
     std::cin >> userInput;
     if (std::cin.fail())
     {
index cf0ff40..0a894c1 100644 (file)
@@ -87,7 +87,7 @@ int processUserInput(int min = std::numeric_limits<int>::min(),
 {
     assert(min <= max);
 
-    int input;
+    int input = 0;
 
     std::cin >> input;
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
@@ -423,16 +423,16 @@ std::string selectResourceType()
 
     switch (processUserInput(RESOURCE_TEMP, RESOURCE_LIGHT))
     {
-    case RESOURCE_TEMP:
-    {
-        g_attrKey = "Temperature";
-        return RESOURCE_TYPE_TEMP;
-    }
-    case RESOURCE_LIGHT:
-    {
-        g_attrKey = "Brightness";
-        return RESOURCE_TYPE_LIGHT;
-    }
+        case RESOURCE_TEMP:
+        {
+            g_attrKey = "Temperature";
+            return RESOURCE_TYPE_TEMP;
+        }
+        case RESOURCE_LIGHT:
+        {
+            g_attrKey = "Brightness";
+            return RESOURCE_TYPE_LIGHT;
+        }
     }
 
     throw std::logic_error("unreachable");
index 65b9cd6..4ac4038 100644 (file)
@@ -53,7 +53,7 @@ int processUserInput(int min, int max)
 {
     assert(min <= max);
 
-    int input;
+    int input = 0;
 
     std::cin >> input;
 
@@ -178,7 +178,7 @@ void runResourceTypeSelection(int resourceMode)
     std::cout << "========================================================\n";
 
     int resourceType = processUserInput(RESOURCE_TEMP, RESOURCE_LIGHT);
-    DisplayControlMenuFunc displayMenuFunc;
+    DisplayControlMenuFunc displayMenuFunc = nullptr;
     std::string attrKey;
 
     switch (resourceType)
index 4c9b4f6..1a76655 100755 (executable)
@@ -70,7 +70,7 @@ int processUserInput(int min, int max)
 {
     assert(min <= max);
 
-    int input;
+    int input = 0;
 
     while(true)
     {
index c91597b..258a28a 100644 (file)
@@ -97,7 +97,7 @@ TEST_F(ExpiryTimerImplTest, CallbackBeInvokedWithinTolerance)
 
 TEST_F(ExpiryTimerImplTest, CallbackBeInvokedWithTimerId)
 {
-    ExpiryTimerImpl::Id returnedId;
+    ExpiryTimerImpl::Id returnedId = 0;
     FunctionObject* functor = mocks.Mock< FunctionObject >();
 
     mocks.ExpectCall(functor, FunctionObject::execute).Match(
@@ -236,7 +236,7 @@ TEST_F(ExpiryTimerTest, CallbackBeInvokedWithinTolerance)
 
 TEST_F(ExpiryTimerTest, CallbackBeInvokedWithTimerId)
 {
-    ExpiryTimer::Id returnedId;
+    ExpiryTimer::Id returnedId = 0;
     FunctionObject* functor = mocks.Mock< FunctionObject >();
 
     mocks.ExpectCall(functor, FunctionObject::execute).Match(
index 478f716..69f1612 100644 (file)
@@ -108,7 +108,7 @@ TEST_F(ResourceBrokerTest,CancelHostResource_NoThrowIfNormalParams)
 
     MockingFunc();
 
-    BrokerID ret;
+    BrokerID ret = 0;
     ret = brokerInstance->hostResource(pResource,cb);
 
     ASSERT_NO_THROW(brokerInstance->cancelHostResource(ret));
@@ -137,7 +137,7 @@ TEST_F(ResourceBrokerTest,getResourceState_ReturnNormalValueIfNormalId)
 
     MockingFunc();
 
-    BrokerID ret;
+    BrokerID ret = 0;
     ret = brokerInstance->hostResource(pResource,cb);
 
     ASSERT_NE(brokerInstance->getResourceState(ret),BROKER_STATE::NONE);
@@ -160,7 +160,7 @@ TEST_F(ResourceBrokerTest,getResourceState_ReturnNormalValueIfNormalResource)
 
     MockingFunc();
 
-    BrokerID ret;
+    BrokerID ret = 0;
     ret = brokerInstance->hostResource(pResource,cb);
 
     ASSERT_NE(brokerInstance->getResourceState(pResource),BROKER_STATE::NONE);
@@ -181,8 +181,8 @@ TEST_F(ResourceBrokerTest,getResourceState_NormalErrorHandlingIfAbnormalResource
 
     MockingFunc();
 
-    PrimitiveResource::Ptr resource[3];
-    BrokerID id[3];
+    PrimitiveResource::Ptr resource[3] = {nullptr,};
+    BrokerID id[3] = {0,};
 
     for(int i=0;i!=3;i++)
     {
index d87d186..0199887 100644 (file)
@@ -97,7 +97,7 @@ TEST_F(ResourcePresenceTest,timeoutCB_TimeOverWhenIsRequestGet)
     MockingFunc();
     instance->initializeResourcePresence(pResource);
     std::cout<<"wait while done timeout requestGet\n";
-    BROKER_STATE state;
+    BROKER_STATE state = BROKER_STATE::NONE;
     state = instance->getResourceState();
     sleep((BROKER_DEVICE_PRESENCE_TIMEROUT/1000)+1);
     ASSERT_EQ(state,instance->getResourceState());