Kaynağa Gözat

fix two memory leaks reported by gcc

Reported-By: gcc -fsanitize=address -fno-sanitize=vptr
Git-Dch: Ignore
David Kalnischkies 10 yıl önce
ebeveyn
işleme
830a1b8c9e
7 değiştirilmiş dosya ile 30 ekleme ve 24 silme
  1. 9 6
      apt-pkg/metaindex.cc
  2. 2 2
      methods/http.cc
  3. 1 1
      methods/http.h
  4. 2 2
      methods/https.cc
  5. 3 2
      methods/https.h
  6. 9 8
      methods/server.cc
  7. 4 3
      methods/server.h

+ 9 - 6
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<pkgIndexFile *>::iterator I = (*Indexes).begin();
-        I != (*Indexes).end(); ++I)
-      delete *I;
-   delete Indexes;
+   if (Indexes != 0)
+   {
+      for (std::vector<pkgIndexFile *>::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					/*{{{*/

+ 2 - 2
methods/http.cc

@@ -778,9 +778,9 @@ bool HttpMethod::Configuration(string Message)
    return true;
 }
 									/*}}}*/
-ServerState * HttpMethod::CreateServerState(URI uri)			/*{{{*/
+std::unique_ptr<ServerState> HttpMethod::CreateServerState(URI const &uri)/*{{{*/
 {
-   return new HttpServerState(uri, this);
+   return std::unique_ptr<ServerState>(new HttpServerState(uri, this));
 }
 									/*}}}*/
 void HttpMethod::RotateDNS()						/*{{{*/

+ 1 - 1
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<ServerState> CreateServerState(URI const &uri) APT_OVERRIDE;
    virtual void RotateDNS() APT_OVERRIDE;
 
    protected:

+ 2 - 2
methods/https.cc

@@ -505,9 +505,9 @@ bool HttpsMethod::Configuration(string Message)
    return true;
 }
 									/*}}}*/
-ServerState * HttpsMethod::CreateServerState(URI uri)			/*{{{*/
+std::unique_ptr<ServerState> HttpsMethod::CreateServerState(URI const &uri)/*{{{*/
 {
-   return new HttpsServerState(uri, this);
+   return std::unique_ptr<ServerState>(new HttpsServerState(uri, this));
 }
 									/*}}}*/
 

+ 3 - 2
methods/https.h

@@ -17,6 +17,7 @@
 #include <iostream>
 #include <stddef.h>
 #include <string>
+#include <memory>
 
 #include "server.h"
 
@@ -67,7 +68,7 @@ class HttpsMethod : public ServerMethod
 				 double ultotal, double ulnow);
    void SetupProxy();
    CURL *curl;
-   ServerState *Server;
+   std::unique_ptr<ServerState> 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<ServerState> CreateServerState(URI const &uri) APT_OVERRIDE;
    using pkgAcqMethod::FetchResult;
    using pkgAcqMethod::FetchItem;
 

+ 9 - 8
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)
+{
+}
+									/*}}}*/

+ 4 - 3
methods/server.h

@@ -17,6 +17,7 @@
 #include <time.h>
 #include <iostream>
 #include <string>
+#include <memory>
 
 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<ServerState> 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<ServerState> 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() {};
 };