From f68167a0e55c7261c9e61f0f39945cd0e908dabb Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 26 Jun 2017 17:47:38 +0200 Subject: 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 --- apt-pkg/contrib/fileutl.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') 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; } -- cgit v1.2.3