From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on sa.local.altlinux.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.1 From: Aleksei Nikiforov To: devel@lists.altlinux.org Date: Thu, 12 Dec 2019 12:57:23 +0300 Message-Id: <20191212095730.83787-15-darktemplar@altlinux.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191212095730.83787-1-darktemplar@altlinux.org> References: <20191211234857.GB17949@altlinux.org> <20191212095730.83787-1-darktemplar@altlinux.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Cc: Aleksei Nikiforov Subject: [devel] [PATCH for apt v2 14/21] Fix memory leaks X-BeenThere: devel@lists.altlinux.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: ALT Linux Team development discussions List-Id: ALT Linux Team development discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 12 Dec 2019 09:58:29 -0000 Archived-At: List-Archive: List-Post: Found via clang-static-analyzer: Memory error: Memory leak: Potential leak of memory pointed to by 'X' --- 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 +#include + 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 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 #include +#include #include #include #include @@ -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 Show(new unsigned char[Cache.Head().PackageCount]); + std::unique_ptr Flags(new unsigned char[Cache.Head().PackageCount]); + std::unique_ptr 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 Show(new unsigned char[Cache.Head().PackageCount]); + std::unique_ptr Flags(new unsigned char[Cache.Head().PackageCount]); + std::unique_ptr 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 +#include #include #include #include @@ -256,7 +257,7 @@ int Score(const string &Path) bool DropRepeats(vector &List,const char *Name) { // Get a list of all the inodes - ino_t *Inodes = new ino_t[List.size()]; + std::unique_ptr 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 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 #include #include +#include #include #include @@ -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 md5cache; map* > 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 l; list *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.1