From 830a1b8c9e9a26dc1101167ac66a75c444902c4d Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Fri, 11 Sep 2015 21:02:19 +0200 Subject: fix two memory leaks reported by gcc Reported-By: gcc -fsanitize=address -fno-sanitize=vptr Git-Dch: Ignore --- apt-pkg/metaindex.cc | 15 +++++++++------ methods/http.cc | 4 ++-- methods/http.h | 2 +- methods/https.cc | 4 ++-- methods/https.h | 5 +++-- methods/server.cc | 17 +++++++++-------- methods/server.h | 7 ++++--- 7 files changed, 30 insertions(+), 24 deletions(-) diff --git a/apt-pkg/metaindex.cc b/apt-pkg/metaindex.cc index 1632b928c..5c095a2ad 100644 --- a/apt-pkg/metaindex.cc +++ b/apt-pkg/metaindex.cc @@ -32,12 +32,15 @@ metaIndex::metaIndex(std::string const &URI, std::string const &Dist, metaIndex::~metaIndex() { - if (Indexes == 0) - return; - for (std::vector::iterator I = (*Indexes).begin(); - I != (*Indexes).end(); ++I) - delete *I; - delete Indexes; + if (Indexes != 0) + { + for (std::vector::iterator I = (*Indexes).begin(); + I != (*Indexes).end(); ++I) + delete *I; + delete Indexes; + } + for (auto const &E: Entries) + delete E.second; } // one line Getters for public fields /*{{{*/ diff --git a/methods/http.cc b/methods/http.cc index ce697a338..78b20e66d 100644 --- a/methods/http.cc +++ b/methods/http.cc @@ -778,9 +778,9 @@ bool HttpMethod::Configuration(string Message) return true; } /*}}}*/ -ServerState * HttpMethod::CreateServerState(URI uri) /*{{{*/ +std::unique_ptr HttpMethod::CreateServerState(URI const &uri)/*{{{*/ { - return new HttpServerState(uri, this); + return std::unique_ptr(new HttpServerState(uri, this)); } /*}}}*/ void HttpMethod::RotateDNS() /*{{{*/ diff --git a/methods/http.h b/methods/http.h index da6139b02..e06a046ef 100644 --- a/methods/http.h +++ b/methods/http.h @@ -128,7 +128,7 @@ class HttpMethod : public ServerMethod virtual bool Configuration(std::string Message) APT_OVERRIDE; - virtual ServerState * CreateServerState(URI uri) APT_OVERRIDE; + virtual std::unique_ptr CreateServerState(URI const &uri) APT_OVERRIDE; virtual void RotateDNS() APT_OVERRIDE; protected: diff --git a/methods/https.cc b/methods/https.cc index d2ddf6fcf..432a64303 100644 --- a/methods/https.cc +++ b/methods/https.cc @@ -505,9 +505,9 @@ bool HttpsMethod::Configuration(string Message) return true; } /*}}}*/ -ServerState * HttpsMethod::CreateServerState(URI uri) /*{{{*/ +std::unique_ptr HttpsMethod::CreateServerState(URI const &uri)/*{{{*/ { - return new HttpsServerState(uri, this); + return std::unique_ptr(new HttpsServerState(uri, this)); } /*}}}*/ diff --git a/methods/https.h b/methods/https.h index 29b20b921..167107ad6 100644 --- a/methods/https.h +++ b/methods/https.h @@ -17,6 +17,7 @@ #include #include #include +#include #include "server.h" @@ -67,7 +68,7 @@ class HttpsMethod : public ServerMethod double ultotal, double ulnow); void SetupProxy(); CURL *curl; - ServerState *Server; + std::unique_ptr Server; // Used by ServerMethods unused by https virtual void SendReq(FetchItem *) APT_OVERRIDE { exit(42); } @@ -77,7 +78,7 @@ class HttpsMethod : public ServerMethod FileFd *File; virtual bool Configuration(std::string Message) APT_OVERRIDE; - virtual ServerState * CreateServerState(URI uri) APT_OVERRIDE; + virtual std::unique_ptr CreateServerState(URI const &uri) APT_OVERRIDE; using pkgAcqMethod::FetchResult; using pkgAcqMethod::FetchItem; diff --git a/methods/server.cc b/methods/server.cc index 1c42c69c2..934ec2abe 100644 --- a/methods/server.cc +++ b/methods/server.cc @@ -495,10 +495,8 @@ int ServerMethod::Loop() // Connect to the server if (Server == 0 || Server->Comp(Queue->Uri) == false) - { - delete Server; Server = CreateServerState(Queue->Uri); - } + /* If the server has explicitly said this is the last connection then we pre-emptively shut down the pipeline and tear down the connection. This will speed up HTTP/1.0 servers a tad @@ -515,8 +513,7 @@ int ServerMethod::Loop() if (Server->Open() == false) { Fail(true); - delete Server; - Server = 0; + Server = nullptr; continue; } @@ -748,9 +745,7 @@ int ServerMethod::Loop() return 0; } /*}}}*/ - /*{{{*/ -unsigned long long -ServerMethod::FindMaximumObjectSizeInQueue() const +unsigned long long ServerMethod::FindMaximumObjectSizeInQueue() const /*{{{*/ { unsigned long long MaxSizeInQueue = 0; for (FetchItem *I = Queue; I != 0 && I != QueueBack; I = I->Next) @@ -758,3 +753,9 @@ ServerMethod::FindMaximumObjectSizeInQueue() const return MaxSizeInQueue; } /*}}}*/ +ServerMethod::ServerMethod(const char *Ver,unsigned long Flags) : /*{{{*/ + pkgAcqMethod(Ver, Flags), Server(nullptr), File(NULL), PipelineDepth(10), + AllowRedirect(false), Debug(false) +{ +} + /*}}}*/ diff --git a/methods/server.h b/methods/server.h index f9f6e9071..5de7686d9 100644 --- a/methods/server.h +++ b/methods/server.h @@ -17,6 +17,7 @@ #include #include #include +#include using std::cout; using std::endl; @@ -108,7 +109,7 @@ class ServerMethod : public pkgAcqMethod protected: virtual bool Fetch(FetchItem *) APT_OVERRIDE; - ServerState *Server; + std::unique_ptr Server; std::string NextURI; FileFd *File; @@ -152,10 +153,10 @@ class ServerMethod : public pkgAcqMethod int Loop(); virtual void SendReq(FetchItem *Itm) = 0; - virtual ServerState * CreateServerState(URI uri) = 0; + virtual std::unique_ptr CreateServerState(URI const &uri) = 0; virtual void RotateDNS() = 0; - ServerMethod(const char *Ver,unsigned long Flags = 0) : pkgAcqMethod(Ver, Flags), Server(NULL), File(NULL), PipelineDepth(10), AllowRedirect(false), Debug(false) {}; + ServerMethod(const char *Ver,unsigned long Flags = 0); virtual ~ServerMethod() {}; }; -- cgit v1.2.3