[devel] [PATCH for apt 14/38] Fix memory leaks
Aleksei Nikiforov
darktemplar на altlinux.org
Вт Дек 10 18:23:19 MSK 2019
Found via clang-static-analyzer
---
apt/apt-pkg/algorithms.cc | 2 +-
apt/apt-pkg/init.cc | 2 +-
apt/apt-pkg/rpm/rpmpackagedata.cc | 8 +++++---
apt/cmdline/apt-cache.cc | 20 ++++++++++++--------
apt/cmdline/apt-cdrom.cc | 3 ++-
apt/cmdline/apt-get.cc | 2 +-
apt/cmdline/apt-shell.cc | 3 +++
apt/methods/rsync.cc | 1 +
apt/tools/gensrclist.cc | 13 ++++++-------
9 files changed, 32 insertions(+), 22 deletions(-)
diff --git a/apt/apt-pkg/algorithms.cc b/apt/apt-pkg/algorithms.cc
index d94d25c..772f609 100644
--- a/apt/apt-pkg/algorithms.cc
+++ b/apt/apt-pkg/algorithms.cc
@@ -1065,7 +1065,7 @@ void pkgProblemResolver::MakeScores()
/* Protected things are pushed really high up. This number should put them
ahead of everything */
- RPMPackageData *rpmdata = new RPMPackageData();
+ RPMPackageData *rpmdata = RPMPackageData::Singleton();
for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
{
if ((Flags[I->ID] & Protected) != 0)
diff --git a/apt/apt-pkg/init.cc b/apt/apt-pkg/init.cc
index 1e1d46b..4f607d4 100644
--- a/apt/apt-pkg/init.cc
+++ b/apt/apt-pkg/init.cc
@@ -42,7 +42,7 @@ bool pkgInitConfig(Configuration &Cnf)
const char *cpu = NULL;
struct utsname name;
if (uname(&name) == 0)
- cpu = strdup(name.machine);
+ cpu = name.machine;
if (cpu == NULL)
cpu = COMMON_CPU;
diff --git a/apt/apt-pkg/rpm/rpmpackagedata.cc b/apt/apt-pkg/rpm/rpmpackagedata.cc
index 186a146..61c15d5 100644
--- a/apt/apt-pkg/rpm/rpmpackagedata.cc
+++ b/apt/apt-pkg/rpm/rpmpackagedata.cc
@@ -14,6 +14,8 @@
#include <rpm/rpmlib.h>
+#include <memory>
+
RPMPackageData::RPMPackageData()
: MinArchScore(-1)
#ifdef WITH_HASH_MAP
@@ -334,10 +336,10 @@ bool RPMPackageData::IsDupPackage(const string &Name)
RPMPackageData *RPMPackageData::Singleton()
{
- static RPMPackageData *data = NULL;
+ static std::unique_ptr<RPMPackageData> data;
if (!data)
- data = new RPMPackageData();
- return data;
+ data.reset(new RPMPackageData());
+ return data.get();
}
#endif /* HAVE_RPM */
diff --git a/apt/cmdline/apt-cache.cc b/apt/cmdline/apt-cache.cc
index 676dda5..89d3367 100644
--- a/apt/cmdline/apt-cache.cc
+++ b/apt/cmdline/apt-cache.cc
@@ -35,6 +35,7 @@
// as reported by Radu Greab.
//#include <locale.h>
#include <iostream>
+#include <memory>
#include <unistd.h>
#include <errno.h>
#include <regex.h>
@@ -965,9 +966,9 @@ bool XVcg(CommandLine &CmdL)
0 = None */
enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
enum TheFlags {ForceNR=(1<<0)};
- unsigned char *Show = new unsigned char[Cache.Head().PackageCount];
- unsigned char *Flags = new unsigned char[Cache.Head().PackageCount];
- unsigned char *ShapeMap = new unsigned char[Cache.Head().PackageCount];
+ std::unique_ptr<unsigned char[]> Show(new unsigned char[Cache.Head().PackageCount]);
+ std::unique_ptr<unsigned char[]> Flags(new unsigned char[Cache.Head().PackageCount]);
+ std::unique_ptr<unsigned char[]> ShapeMap(new unsigned char[Cache.Head().PackageCount]);
// Show everything if no arguments given
if (CmdL.FileList[1] == 0)
@@ -976,7 +977,7 @@ bool XVcg(CommandLine &CmdL)
else
for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
Show[I] = None;
- memset(Flags,0,sizeof(*Flags)*Cache.Head().PackageCount);
+ memset(Flags.get(),0,sizeof(unsigned char*)*Cache.Head().PackageCount);
// Map the shapes
for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
@@ -1183,9 +1184,9 @@ bool Dotty(CommandLine &CmdL)
0 = None */
enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
enum TheFlags {ForceNR=(1<<0)};
- unsigned char *Show = new unsigned char[Cache.Head().PackageCount];
- unsigned char *Flags = new unsigned char[Cache.Head().PackageCount];
- unsigned char *ShapeMap = new unsigned char[Cache.Head().PackageCount];
+ std::unique_ptr<unsigned char[]> Show(new unsigned char[Cache.Head().PackageCount]);
+ std::unique_ptr<unsigned char[]> Flags(new unsigned char[Cache.Head().PackageCount]);
+ std::unique_ptr<unsigned char[]> ShapeMap(new unsigned char[Cache.Head().PackageCount]);
// Show everything if no arguments given
if (CmdL.FileList[1] == 0)
@@ -1194,7 +1195,7 @@ bool Dotty(CommandLine &CmdL)
else
for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
Show[I] = None;
- memset(Flags,0,sizeof(*Flags)*Cache.Head().PackageCount);
+ memset(Flags.get(),0,sizeof(unsigned char*)*Cache.Head().PackageCount);
// Map the shapes
for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
@@ -1504,6 +1505,7 @@ bool Search(CommandLine &CmdL)
{
for (; I != 0; I--)
regfree(&Patterns[I]);
+ delete [] Patterns;
return _error->Error("Regex compilation error");
}
}
@@ -1514,6 +1516,7 @@ bool Search(CommandLine &CmdL)
{
for (unsigned I = 0; I != NumPatterns; I++)
regfree(&Patterns[I]);
+ delete [] Patterns;
return false;
}
@@ -1601,6 +1604,7 @@ bool Search(CommandLine &CmdL)
delete [] VFList;
for (unsigned I = 0; I != NumPatterns; I++)
regfree(&Patterns[I]);
+ delete [] Patterns;
if (ferror(stdout))
return _error->Error("Write to stdout failed");
return true;
diff --git a/apt/cmdline/apt-cdrom.cc b/apt/cmdline/apt-cdrom.cc
index 93ad41d..e34ec95 100644
--- a/apt/cmdline/apt-cdrom.cc
+++ b/apt/cmdline/apt-cdrom.cc
@@ -30,6 +30,7 @@
// CNC:2003-02-14 - apti18n.h includes libintl.h which includes locale.h,
// as reported by Radu Greab.
//#include <locale.h>
+#include <memory>
#include <iostream>
#include <fstream>
#include <vector>
@@ -256,7 +257,7 @@ int Score(const string &Path)
bool DropRepeats(vector<string> &List,const char *Name)
{
// Get a list of all the inodes
- ino_t *Inodes = new ino_t[List.size()];
+ std::unique_ptr<ino_t[]> Inodes(new ino_t[List.size()]);
for (unsigned int I = 0; I != List.size(); I++)
{
struct stat Buf;
diff --git a/apt/cmdline/apt-get.cc b/apt/cmdline/apt-get.cc
index 1eff3eb..5484b95 100644
--- a/apt/cmdline/apt-get.cc
+++ b/apt/cmdline/apt-get.cc
@@ -1910,7 +1910,7 @@ bool DoSource(CommandLine &CmdL)
AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
pkgAcquire Fetcher(&Stat);
- DscFile *Dsc = new DscFile[CmdL.FileSize()];
+ std::unique_ptr<DscFile[]> Dsc(new DscFile[CmdL.FileSize()]);
// Load the requestd sources into the fetcher
unsigned J = 0;
diff --git a/apt/cmdline/apt-shell.cc b/apt/cmdline/apt-shell.cc
index 951bc3c..3a6748b 100644
--- a/apt/cmdline/apt-shell.cc
+++ b/apt/cmdline/apt-shell.cc
@@ -2780,6 +2780,7 @@ bool Search(CommandLine &CmdL)
{
for (; I != 0; I--)
regfree(&Patterns[I]);
+ delete [] Patterns;
return _error->Error("Regex compilation error");
}
}
@@ -2790,6 +2791,7 @@ bool Search(CommandLine &CmdL)
{
for (unsigned I = 0; I != NumPatterns; I++)
regfree(&Patterns[I]);
+ delete [] Patterns;
return false;
}
@@ -2877,6 +2879,7 @@ bool Search(CommandLine &CmdL)
delete [] VFList;
for (unsigned I = 0; I != NumPatterns; I++)
regfree(&Patterns[I]);
+ delete [] Patterns;
if (ferror(stdout))
return _error->Error("Write to stdout failed");
return true;
diff --git a/apt/methods/rsync.cc b/apt/methods/rsync.cc
index 2b318fd..1fc6a36 100644
--- a/apt/methods/rsync.cc
+++ b/apt/methods/rsync.cc
@@ -79,6 +79,7 @@ bool Argv::resize()
char **new_args = new char *[max_size+increment];
memcpy(new_args,args,size*sizeof(char*));
memset(new_args+size,0, (max_size+increment-size) * sizeof(char*));
+ delete [] args;
args = new_args;
max_size += increment;
return true;
diff --git a/apt/tools/gensrclist.cc b/apt/tools/gensrclist.cc
index 576a972..00d4912 100644
--- a/apt/tools/gensrclist.cc
+++ b/apt/tools/gensrclist.cc
@@ -21,6 +21,7 @@
#include <map>
#include <list>
#include <iostream>
+#include <memory>
#include <apt-pkg/error.h>
#include <apt-pkg/tagfile.h>
@@ -154,7 +155,7 @@ int main(int argc, char ** argv)
Header h;
int32_t size[1];
int entry_no, entry_cur;
- CachedMD5 *md5cache;
+ std::unique_ptr<CachedMD5> md5cache;
map<string, list<char*>* > rpmTable; // table that maps srpm -> generated rpm
bool mapi = false;
bool progressBar = false;
@@ -206,7 +207,7 @@ int main(int argc, char ** argv)
if (!readRPMTable(arg_srpmindex, rpmTable))
exit(1);
- md5cache = new CachedMD5(string(arg_dir)+string(arg_suffix), "gensrclist");
+ md5cache.reset(new CachedMD5(string(arg_dir)+string(arg_suffix), "gensrclist"));
getcwd(cwd, 200);
if (*arg_dir != '/') {
@@ -358,11 +359,11 @@ int main(int argc, char ** argv)
foundInIndex = false;
{
int count = 0;
- char **l = NULL;
+ std::unique_ptr<char* []> l;
list<char*> *rpmlist = rpmTable[string(dirEntries[entry_cur]->d_name)];
if (rpmlist) {
- l = new char *[rpmlist->size()];
+ l.reset(new char *[rpmlist->size()]);
foundInIndex = true;
@@ -375,7 +376,7 @@ int main(int argc, char ** argv)
if (count) {
headerAddEntry(newHeader, CRPMTAG_BINARY,
- RPM_STRING_ARRAY_TYPE, l, count);
+ RPM_STRING_ARRAY_TYPE, l.get(), count);
}
}
if (foundInIndex || !mapi)
@@ -399,8 +400,6 @@ int main(int argc, char ** argv)
ts = rpmtsFree(ts);
#endif
- delete md5cache;
-
return 0;
}
--
2.24.0
Подробная информация о списке рассылки Devel