Blame SOURCES/0022-Fix-few-memory-leaks.patch

121cca
From 71cf63b7bb7bb212580cc7c8e6c75a4f645d79f1 Mon Sep 17 00:00:00 2001
121cca
From: Shivaprasad G Bhat <sbhat@linux.ibm.com>
121cca
Date: Wed, 20 May 2020 06:27:05 -0500
121cca
Subject: [PATCH 22/65] Fix few memory leaks
121cca
121cca
Valgrind showed some leaks with scandir usage in sysfs.cc.
121cca
Patch audits all the scandir usage and fixes where required.
121cca
121cca
Signed-off-by: Shivaprasad G Bhat <sbhat@linux.ibm.com>
121cca
---
121cca
 src/core/parisc.cc | 10 ++++++----
121cca
 src/core/spd.cc    |  4 ++++
121cca
 src/core/sysfs.cc  | 32 ++++++++++++++++++++++++++++----
121cca
 3 files changed, 38 insertions(+), 8 deletions(-)
121cca
121cca
diff --git a/src/core/parisc.cc b/src/core/parisc.cc
121cca
index 80344d2..1e531e3 100644
121cca
--- a/src/core/parisc.cc
121cca
+++ b/src/core/parisc.cc
121cca
@@ -561,11 +561,13 @@ static bool scan_device(hwNode & node, string name = "")
121cca
   else
121cca
   {
121cca
     for (int i = 0; i < n; i++)
121cca
-      if(matches(namelist[i]->d_name, "^[0-9]+(:[0-9]+)*$"))
121cca
     {
121cca
-      pushd(namelist[i]->d_name);
121cca
-      scan_device(curnode?*curnode:node, namelist[i]->d_name);
121cca
-      popd();
121cca
+      if(matches(namelist[i]->d_name, "^[0-9]+(:[0-9]+)*$"))
121cca
+      {
121cca
+        pushd(namelist[i]->d_name);
121cca
+        scan_device(curnode?*curnode:node, namelist[i]->d_name);
121cca
+        popd();
121cca
+      }
121cca
       free(namelist[i]);
121cca
     }
121cca
     free(namelist);
121cca
diff --git a/src/core/spd.cc b/src/core/spd.cc
121cca
index 061d0fd..a304d06 100644
121cca
--- a/src/core/spd.cc
121cca
+++ b/src/core/spd.cc
121cca
@@ -192,8 +192,12 @@ static bool scan_eeproms(hwNode & memory)
121cca
     return false;
121cca
 
121cca
   for (int i = 0; i < n; i++)
121cca
+  {
121cca
     if (scan_eeprom(memory, namelist[i]->d_name))
121cca
       current_bank++;
121cca
+    free(namelist[i]);
121cca
+  }
121cca
+  free(namelist);
121cca
 
121cca
   return true;
121cca
 }
121cca
diff --git a/src/core/sysfs.cc b/src/core/sysfs.cc
121cca
index ee8b1da..4e2df1c 100644
121cca
--- a/src/core/sysfs.cc
121cca
+++ b/src/core/sysfs.cc
121cca
@@ -83,7 +83,7 @@ static string sysfs_getbustype(const string & path)
121cca
 {
121cca
   struct dirent **namelist;
121cca
   int i, n;
121cca
-  string devname;
121cca
+  string bustype = "";
121cca
 
121cca
 /*
121cca
   to determine to which kind of bus a device is connected:
121cca
@@ -96,17 +96,28 @@ static string sysfs_getbustype(const string & path)
121cca
   n = scandir(".", &namelist, selectdir, alphasort);
121cca
   popd();
121cca
 
121cca
+  if (n <= 0)
121cca
+    return "";
121cca
+
121cca
   for (i = 0; i < n; i++)
121cca
   {
121cca
-    devname =
121cca
+    string devname =
121cca
       string(fs.path + "/bus/") + string(namelist[i]->d_name) +
121cca
       "/devices/" + basename(path.c_str());
121cca
 
121cca
     if (samefile(devname, path))
121cca
-      return string(namelist[i]->d_name);
121cca
+    {
121cca
+      bustype = string(namelist[i]->d_name);
121cca
+      break;
121cca
+    }
121cca
+    free(namelist[i]);
121cca
   }
121cca
 
121cca
-  return "";
121cca
+  for (int j = i; j < n; j++)
121cca
+    free(namelist[j]);
121cca
+  free(namelist);
121cca
+
121cca
+  return bustype;
121cca
 }
121cca
 
121cca
 
121cca
@@ -405,7 +416,11 @@ vector < entry > entry::devices() const
121cca
     entry e = sysfs::entry(This->devpath + "/" + string(namelist[i]->d_name));
121cca
     if(e.hassubdir("subsystem"))
121cca
 	    result.push_back(e);
121cca
+    free(namelist[i]);
121cca
   }
121cca
+  if (namelist)
121cca
+    free(namelist);
121cca
+
121cca
   if(pushd("block"))
121cca
   {
121cca
     int count = scandir(".", &namelist, selectdir, alphasort);
121cca
@@ -414,7 +429,10 @@ vector < entry > entry::devices() const
121cca
       entry e = sysfs::entry(This->devpath + "/block/" + string(namelist[i]->d_name));
121cca
       if(e.hassubdir("subsystem"))
121cca
 	      result.push_back(e);
121cca
+      free(namelist[i]);
121cca
     }
121cca
+    if (namelist)
121cca
+      free(namelist);
121cca
     popd();
121cca
   }
121cca
   popd();
121cca
@@ -435,8 +453,11 @@ vector < entry > sysfs::entries_by_bus(const string & busname)
121cca
   {
121cca
     entry e = sysfs::entry::byBus(busname, namelist[i]->d_name);
121cca
     result.push_back(e);
121cca
+    free(namelist[i]);
121cca
   }
121cca
   popd();
121cca
+  if (namelist)
121cca
+    free(namelist);
121cca
   return result;
121cca
 }
121cca
 
121cca
@@ -454,8 +475,11 @@ vector < entry > sysfs::entries_by_class(const string & classname)
121cca
   {
121cca
     entry e = sysfs::entry::byClass(classname, namelist[i]->d_name);
121cca
     result.push_back(e);
121cca
+    free(namelist[i]);
121cca
   }
121cca
   popd();
121cca
+  if (namelist)
121cca
+    free(namelist);
121cca
   return result;
121cca
 }
121cca
 
121cca
-- 
121cca
2.33.1
121cca