Fixed a few SonarQube identified issues
authorErich Keane <erich.keane@intel.com>
Fri, 31 Jul 2015 22:55:45 +0000 (15:55 -0700)
committerJon A. Cruz <jonc@osg.samsung.com>
Sat, 1 Aug 2015 00:39:29 +0000 (00:39 +0000)
JniListenerManager tried to modify its list inside of a for-loop,
which causes undefined behavior.  2 spots where there was un-initialized
variables.

Change-Id: I5da2cd39cef1dadcd05c743d8fc5e0db0a8f2679
Signed-off-by: Erich Keane <erich.keane@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2033
Reviewed-by: Tim Kourt <tim.a.kourt@intel.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
android/android_api/base/jni/JniListenerManager.h
android/android_api/base/jni/JniOcPlatform.cpp
resource/csdk/ocrandom/src/ocrandom.c

index 709328a..52eddc3 100644 (file)
@@ -58,7 +58,9 @@ public:
 
             if (jgListener)
             {
-                m_listenerMap.insert(std::pair<jobject, std::pair<T*, int>>(jgListener, std::pair<T*, int>(onEventListener, 1)));
+                m_listenerMap.insert(
+                        std::pair<jobject,
+                        std::pair<T*, int>>(jgListener, std::pair<T*, int>(onEventListener, 1)));
             }
             else
             {
@@ -105,14 +107,13 @@ public:
     {
         m_mapMutex.lock();
 
-        for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it)
+        for (auto& pair : m_listenerMap)
         {
-            env->DeleteGlobalRef(it->first);
-            auto refPair = it->second;
-            T* listener = refPair.first;
-            delete listener;
-            m_listenerMap.erase(it);
+            env->DeleteGlobalRef(pair.first);
+            auto refPair = pair.second;
+            delete refPair.first;
         }
+        m_listenerMap.clear();
 
         m_mapMutex.unlock();
     }
index c8a619e..3b67b6c 100644 (file)
@@ -325,7 +325,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_configure
         dbfile = env->GetStringUTFChars(jDbPath, nullptr);
         JniOcSecurity::StoreDbPath(dbfile);
     }
-    uint16_t port;
+    uint16_t port = 0;
     if (jPort > 0)
     {
         port = static_cast<uint16_t>(jPort);
index dd03cd9..0d0f0c1 100644 (file)
@@ -70,7 +70,7 @@ int8_t OCSeedRandom() {
 #if defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__)
     int32_t fd = open("/dev/urandom", O_RDONLY);
     if (fd > 0) {
-        uint32_t randomSeed;
+        uint32_t randomSeed = 0;
         uint32_t totalRead = 0; //how many integers were read
         int32_t currentRead = 0;
         while (totalRead < sizeof(randomSeed)) {