diff options
author | David Kalnischkies <david@kalnischkies.de> | 2017-06-26 17:47:38 +0200 |
---|---|---|
committer | David Kalnischkies <david@kalnischkies.de> | 2017-06-26 23:31:15 +0200 |
commit | f68167a0e55c7261c9e61f0f39945cd0e908dabb (patch) | |
tree | b7a2c179357e56be0248b80c8362dedcff87c40d | |
parent | 42654d08c2ca1bee18b6947a39228a35c2409deb (diff) |
fix some unlikely memory leaks in error cases
The error cases are just as unlikely as the memory leaks to ever cause
real problems, but lets play it safe for correctness.
Reported-By: scan-build & clang
Gbp-Dch: Ignore
-rw-r--r-- | apt-pkg/contrib/fileutl.cc | 10 | ||||
-rw-r--r-- | apt-pkg/deb/debindexfile.cc | 33 | ||||
-rw-r--r-- | apt-pkg/edsp/edspindexfile.cc | 13 | ||||
-rw-r--r-- | apt-pkg/indexfile.cc | 10 |
4 files changed, 24 insertions, 42 deletions
diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 8ce8d5baf..e87102cbc 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -2847,7 +2847,7 @@ std::string GetTempDir(std::string const &User) FileFd* GetTempFile(std::string const &Prefix, bool ImmediateUnlink, FileFd * const TmpFd) /*{{{*/ { char fn[512]; - FileFd * const Fd = TmpFd == NULL ? new FileFd() : TmpFd; + FileFd * const Fd = TmpFd == nullptr ? new FileFd() : TmpFd; std::string const tempdir = GetTempDir(); snprintf(fn, sizeof(fn), "%s/%s.XXXXXX", @@ -2858,12 +2858,16 @@ FileFd* GetTempFile(std::string const &Prefix, bool ImmediateUnlink, FileFd * co if (fd < 0) { _error->Errno("GetTempFile",_("Unable to mkstemp %s"), fn); - return NULL; + if (TmpFd == nullptr) + delete Fd; + return nullptr; } if (!Fd->OpenDescriptor(fd, FileFd::ReadWrite, FileFd::None, true)) { _error->Errno("GetTempFile",_("Unable to write to %s"),fn); - return NULL; + if (TmpFd == nullptr) + delete Fd; + return nullptr; } return Fd; } diff --git a/apt-pkg/deb/debindexfile.cc b/apt-pkg/deb/debindexfile.cc index c55847305..799144ecb 100644 --- a/apt-pkg/deb/debindexfile.cc +++ b/apt-pkg/deb/debindexfile.cc @@ -26,6 +26,7 @@ #include <stdio.h> #include <iostream> +#include <memory> #include <string> #include <sstream> @@ -66,7 +67,7 @@ bool debSourcesIndex::OpenListFile(FileFd &, std::string const &) } pkgCacheListParser * debSourcesIndex::CreateListParser(FileFd &) { - return NULL; + return nullptr; } uint8_t debSourcesIndex::GetIndexFlags() const { @@ -128,16 +129,10 @@ pkgCacheListParser * debTranslationsIndex::CreateListParser(FileFd &Pkg) if (Pkg.IsOpen() == false) return nullptr; _error->PushToStack(); - pkgCacheListParser * const Parser = new debTranslationsParser(&Pkg); + std::unique_ptr<pkgCacheListParser> Parser(new debTranslationsParser(&Pkg)); bool const newError = _error->PendingError(); _error->MergeWithStack(); - if (newError) - { - delete Parser; - return nullptr; - } - else - return Parser; + return newError ? nullptr : Parser.release(); } /*}}}*/ // dpkg/status Index /*{{{*/ @@ -162,16 +157,10 @@ pkgCacheListParser * debStatusIndex::CreateListParser(FileFd &Pkg) if (Pkg.IsOpen() == false) return nullptr; _error->PushToStack(); - pkgCacheListParser * const Parser = new debStatusListParser(&Pkg); + std::unique_ptr<pkgCacheListParser> Parser(new debStatusListParser(&Pkg)); bool const newError = _error->PendingError(); _error->MergeWithStack(); - if (newError) - { - delete Parser; - return nullptr; - } - else - return Parser; + return newError ? nullptr : Parser.release(); } /*}}}*/ // DebPkgFile Index - a single .deb file as an index /*{{{*/ @@ -244,16 +233,10 @@ pkgCacheListParser * debDebPkgFileIndex::CreateListParser(FileFd &Pkg) if (Pkg.IsOpen() == false) return nullptr; _error->PushToStack(); - pkgCacheListParser * const Parser = new debDebFileParser(&Pkg, DebFile); + std::unique_ptr<pkgCacheListParser> Parser(new debDebFileParser(&Pkg, DebFile)); bool const newError = _error->PendingError(); _error->MergeWithStack(); - if (newError) - { - delete Parser; - return nullptr; - } - else - return Parser; + return newError ? nullptr : Parser.release(); } uint8_t debDebPkgFileIndex::GetIndexFlags() const { diff --git a/apt-pkg/edsp/edspindexfile.cc b/apt-pkg/edsp/edspindexfile.cc index 042a88cf9..2d030daaf 100644 --- a/apt-pkg/edsp/edspindexfile.cc +++ b/apt-pkg/edsp/edspindexfile.cc @@ -18,6 +18,7 @@ #include <stddef.h> #include <unistd.h> +#include <memory> #include <string> /*}}}*/ @@ -61,12 +62,12 @@ std::string edspIndex::GetComponent() const pkgCacheListParser * edspIndex::CreateListParser(FileFd &Pkg) { if (Pkg.IsOpen() == false) - return NULL; + return nullptr; _error->PushToStack(); - pkgCacheListParser * const Parser = new edspListParser(&Pkg); + std::unique_ptr<pkgCacheListParser> Parser(new edspListParser(&Pkg)); bool const newError = _error->PendingError(); _error->MergeWithStack(); - return newError ? NULL : Parser; + return newError ? nullptr : Parser.release(); } /*}}}*/ // EIPP Index /*{{{*/ @@ -80,12 +81,12 @@ std::string eippIndex::GetComponent() const pkgCacheListParser * eippIndex::CreateListParser(FileFd &Pkg) { if (Pkg.IsOpen() == false) - return NULL; + return nullptr; _error->PushToStack(); - pkgCacheListParser * const Parser = new eippListParser(&Pkg); + std::unique_ptr<pkgCacheListParser> Parser(new eippListParser(&Pkg)); bool const newError = _error->PendingError(); _error->MergeWithStack(); - return newError ? NULL : Parser; + return newError ? nullptr : Parser.release(); } /*}}}*/ diff --git a/apt-pkg/indexfile.cc b/apt-pkg/indexfile.cc index 934943205..b860e2335 100644 --- a/apt-pkg/indexfile.cc +++ b/apt-pkg/indexfile.cc @@ -338,16 +338,10 @@ pkgCacheListParser * pkgDebianIndexFile::CreateListParser(FileFd &Pkg) if (Pkg.IsOpen() == false) return nullptr; _error->PushToStack(); - pkgCacheListParser * const Parser = new debListParser(&Pkg); + std::unique_ptr<pkgCacheListParser> Parser(new debListParser(&Pkg)); bool const newError = _error->PendingError(); _error->MergeWithStack(); - if (newError) - { - delete Parser; - return nullptr; - } - else - return Parser; + return newError ? nullptr : Parser.release(); } bool pkgDebianIndexFile::Merge(pkgCacheGenerator &Gen,OpProgress * const Prog) { |