From 370f09be8fc22e0ba99203808f94c79a15d40db5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rafa=C3=ABl=20Carr=C3=A9?=
 <rafael.carre@savoirfairelinux.com>
Date: Fri, 8 Jul 2011 10:03:00 -0400
Subject: [PATCH] * #6392: fix memory leak (opendir() without closedir())

Also remove 2 levels of indentation to make to code more readable

bug found by cppcheck
---
 sflphone-common/src/plug-in/pluginmanager.cpp | 57 ++++++++++---------
 1 file changed, 30 insertions(+), 27 deletions(-)

diff --git a/sflphone-common/src/plug-in/pluginmanager.cpp b/sflphone-common/src/plug-in/pluginmanager.cpp
index 4789973859..25a809db4c 100644
--- a/sflphone-common/src/plug-in/pluginmanager.cpp
+++ b/sflphone-common/src/plug-in/pluginmanager.cpp
@@ -87,35 +87,38 @@ PluginManager::loadPlugins (const std::string &path)
     dir = opendir (pluginDir.c_str());
     /* Test if the directory exists or is readable */
 
-    if (dir) {
-        /* Read the directory */
-        while ( (dirStruct=readdir (dir))) {
-            /* Get the name of the current item in the directory */
-            current = dirStruct->d_name;
-            /* Test if the current item is not the parent or the current directory and that it ends with .so*/
-
-            if (current != pDir && current != cDir and hasSharedExtension(current)) {
-
-                /* Load the dynamic library */
-                library = loadDynamicLibrary (pluginDir + current);
-
-                /* Instanciate the plugin object */
-
-                if (instanciatePlugin (library, &plugin) != 0) {
-                    _debug ("Error instanciating the plugin ...");
-                    return 1;
-                }
-
-                /* Regitering the current plugin */
-                if (registerPlugin (plugin, library) != 0) {
-                    _debug ("Error registering the plugin ...");
-                    return 1;
-                }
-            }
-        }
-    } else
+    if (!dir)
         return 1;
 
+    /* Read the directory */
+    while ( (dirStruct=readdir (dir))) {
+        /* Get the name of the current item in the directory */
+        current = dirStruct->d_name;
+
+        /* Test if the current item is not the parent or the current directory and that it ends with .so*/
+        if (current == pDir || current == cDir || !hasSharedExtension(current))
+            continue;
+
+
+        /* Load the dynamic library */
+        library = loadDynamicLibrary (pluginDir + current);
+
+        /* Instanciate the plugin object */
+
+        if (instanciatePlugin (library, &plugin) != 0) {
+            _debug ("Error instanciating the plugin ...");
+            closedir(dir);
+            return 1;
+        }
+
+        /* Regitering the current plugin */
+        if (registerPlugin (plugin, library) != 0) {
+            _debug ("Error registering the plugin ...");
+            closedir(dir);
+            return 1;
+        }
+    }
+
     /* Close the directory */
     closedir (dir);
 
-- 
GitLab