From: Aleksei Nikiforov <darktemplar@altlinux.org> To: devel@lists.altlinux.org Cc: Aleksei Nikiforov <darktemplar@altlinux.org> Subject: [devel] [PATCH for apt 14/38] Fix memory leaks Date: Tue, 10 Dec 2019 18:23:19 +0300 Message-ID: <20191210152343.33867-15-darktemplar@altlinux.org> (raw) In-Reply-To: <20191210152343.33867-1-darktemplar@altlinux.org> 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
next prev parent reply other threads:[~2019-12-10 15:23 UTC|newest] Thread overview: 84+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-12-10 15:23 [devel] [PATCH for apt 00/38] Various fixes for Apt and basic integration test suite Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 01/38] Replace post-increments with pre-increments Aleksei Nikiforov 2019-12-10 18:21 ` Alexey Tourbin 2019-12-11 7:51 ` Aleksei Nikiforov 2019-12-11 8:11 ` Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 02/38] Use correct types and type specifiers Aleksei Nikiforov 2019-12-10 22:45 ` Dmitry V. Levin 2019-12-11 7:56 ` Aleksei Nikiforov 2019-12-11 23:48 ` Dmitry V. Levin 2019-12-12 9:57 ` [devel] [PATCH for apt v2 00/21] Various fixes for Apt and basic integration test suite Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 01/21] Replace post-increments with pre-increments Aleksei Nikiforov 2019-12-12 21:32 ` Andrey Savchenko 2019-12-13 8:29 ` Aleksei Nikiforov 2019-12-13 9:11 ` [devel] [PATCH for apt v3 " Aleksei Nikiforov 2019-12-13 9:12 ` [devel] [PATCH for apt v4 09/21] Rework identical conditions Aleksei Nikiforov 2019-12-13 9:18 ` [devel] [PATCH for apt v4] Additional loops improvements and beautification Aleksei Nikiforov 2019-12-13 9:20 ` Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 02/21] Use correct types and type specifiers Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 03/21] Fix potential memory corruption in pkgCache::DepIterator::AllTargets() Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 04/21] Get rid of nullptr dereference Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 05/21] Fix memory access outside of allocated buffer ranges Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 06/21] Get rid of hardcoded array size Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 07/21] Use signed types to detect IO errors Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 08/21] Use 'explicit' keyword for constructors with one argument Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 09/21] Rework identical conditions Aleksei Nikiforov 2019-12-12 19:55 ` Andrey Savchenko 2019-12-13 8:10 ` Aleksei Nikiforov 2019-12-13 8:21 ` [devel] [PATCH for apt v3 " Aleksei Nikiforov 2019-12-13 8:23 ` [devel] [PATCH for apt v3] DoList: optimize status modification loop Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 10/21] Improve member variable initialization Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 11/21] Remove useless variable shadowing Aleksei Nikiforov 2019-12-12 21:19 ` Andrey Savchenko 2019-12-12 9:57 ` [devel] [PATCH for apt v2 12/21] Fortify against buffer overflows Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 13/21] Cacheiterators: sanitize increment operators and end() function Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 14/21] Fix memory leaks Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 15/21] Fix incorrect delete operator Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 16/21] Don't access uninitialized data Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 17/21] Fix access after free error Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 18/21] Apt-pipe: ensure mainloop function doesn't return uninitialized or garbage value Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 19/21] Don't use uninitialized value Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 20/21] Get rid of dangling invalid pointer Aleksei Nikiforov 2019-12-12 9:57 ` [devel] [PATCH for apt v2 21/21] Fix invalid check of Queue against zero Aleksei Nikiforov 2019-12-12 19:08 ` Andrey Savchenko 2019-12-13 7:25 ` Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 03/38] Fix potential memory corruption in pkgCache::DepIterator::AllTargets() Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 04/38] Get rid of nullptr dereference Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 05/38] Fix memory access outside of allocated buffer ranges Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 06/38] Get rid of hardcoded array size Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 07/38] Use signed types to detect IO errors Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 08/38] Use 'explicit' keyword for constructors with one argument Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 09/38] Rework identical conditions Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 10/38] Improve member variable initialization Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 11/38] Remove useless variable shadowing Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 12/38] Fortify against buffer overflows Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 13/38] Cacheiterators: sanitize increment operators and end() function Aleksei Nikiforov 2019-12-10 15:23 ` Aleksei Nikiforov [this message] 2019-12-10 15:23 ` [devel] [PATCH for apt 15/38] Fix incorrect delete operator Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 16/38] Don't access uninitialized data Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 17/38] Fix access after free error Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 18/38] Apt-pipe: ensure mainloop function doesn't return uninitialized or garbage value Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 19/38] Don't use uninitialized value Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 20/38] Get rid of dangling invalid pointer Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 21/38] Fix invalid check of Queue against zero Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 22/38] Fix iterators comparison Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 23/38] Fortify ParseQuoteWord function Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 24/38] Improve ipv6 address handling Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 25/38] Check subsecond modification time for cached files Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 26/38] Fix warning about truncation of value of pkgCache::Header::HeaderSz Aleksei Nikiforov 2019-12-10 22:57 ` Dmitry V. Levin 2019-12-11 7:51 ` Aleksei Nikiforov 2019-12-11 23:41 ` Dmitry V. Levin 2019-12-12 7:38 ` Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 27/38] FileFd: all files are closed automatically Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 28/38] Fix resource leaks in pkgCacheFile class Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 29/38] Fix off by one error in dynamic mmap leading to resource leak Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 30/38] pkgCacheFile: call Close() function in destructor Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 31/38] pkgCacheFile: don't regenerate cache if it was already built Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 32/38] Add support for rpm's dbpath configuration Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 33/38] Import integration tests framework from Debian Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 34/38] Add initial integration tests Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 35/38] Add basic tests to ensure that lua and rpm scripts are called Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 36/38] Add http and https methods tests Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 37/38] Enable integration test suite during package build Aleksei Nikiforov 2019-12-10 15:23 ` [devel] [PATCH for apt 38/38] Add code coverage support Aleksei Nikiforov
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20191210152343.33867-15-darktemplar@altlinux.org \ --to=darktemplar@altlinux.org \ --cc=devel@lists.altlinux.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
ALT Linux Team development discussions This inbox may be cloned and mirrored by anyone: git clone --mirror http://lore.altlinux.org/devel/0 devel/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 devel devel/ http://lore.altlinux.org/devel \ devel@altlinux.org devel@altlinux.ru devel@lists.altlinux.org devel@lists.altlinux.ru devel@linux.iplabs.ru mandrake-russian@linuxteam.iplabs.ru sisyphus@linuxteam.iplabs.ru public-inbox-index devel Example config snippet for mirrors. Newsgroup available over NNTP: nntp://lore.altlinux.org/org.altlinux.lists.devel AGPL code for this site: git clone https://public-inbox.org/public-inbox.git