Ver código fonte

* apt-pkg/deb/dpkgpm.cc:
- use std::vector instead of fixed size arrays to store args and
multiarch-packagename strings

David Kalnischkies 15 anos atrás
pai
commit
edca7af056
2 arquivos alterados com 65 adições e 62 exclusões
  1. 61 61
      apt-pkg/deb/dpkgpm.cc
  2. 4 1
      debian/changelog

+ 61 - 61
apt-pkg/deb/dpkgpm.cc

@@ -927,10 +927,9 @@ bool pkgDPkgPM::Go(int OutStatusFd)
 	    /* nothing */;
 	    /* nothing */;
 
 
       // Generate the argument list
       // Generate the argument list
-      const char *Args[MaxArgs + 50];
+      std::vector<const char *> Args;
       // keep track of allocated strings for multiarch package names
       // keep track of allocated strings for multiarch package names
-      char *Packages[MaxArgs + 50];
-      unsigned int pkgcount = 0;
+      std::vector<char *> Packages;
 
 
       // Now check if we are within the MaxArgs limit
       // Now check if we are within the MaxArgs limit
       //
       //
@@ -940,13 +939,19 @@ bool pkgDPkgPM::Go(int OutStatusFd)
       // - with the split they may now be configured in different
       // - with the split they may now be configured in different
       //   runs, using Immediate-Configure-All can help prevent this.
       //   runs, using Immediate-Configure-All can help prevent this.
       if (J - I > (signed)MaxArgs)
       if (J - I > (signed)MaxArgs)
+      {
 	 J = I + MaxArgs;
 	 J = I + MaxArgs;
-      
-      unsigned int n = 0;
+	 Args.reserve(MaxArgs + 10);
+      }
+      else
+      {
+	 Args.reserve((J - I) + 10);
+      }
+
       unsigned long Size = 0;
       unsigned long Size = 0;
       string const Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
       string const Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
-      Args[n++] = Tmp.c_str();
-      Size += strlen(Args[n-1]);
+      Args.push_back(Tmp.c_str());
+      Size += Tmp.length();
       
       
       // Stick in any custom dpkg options
       // Stick in any custom dpkg options
       Configuration::Item const *Opts = _config->Tree("DPkg::Options");
       Configuration::Item const *Opts = _config->Tree("DPkg::Options");
@@ -957,76 +962,64 @@ bool pkgDPkgPM::Go(int OutStatusFd)
 	 {
 	 {
 	    if (Opts->Value.empty() == true)
 	    if (Opts->Value.empty() == true)
 	       continue;
 	       continue;
-	    Args[n++] = Opts->Value.c_str();
+	    Args.push_back(Opts->Value.c_str());
 	    Size += Opts->Value.length();
 	    Size += Opts->Value.length();
 	 }	 
 	 }	 
       }
       }
       
       
-      char status_fd_buf[20];
       int fd[2];
       int fd[2];
       pipe(fd);
       pipe(fd);
-      
-      Args[n++] = "--status-fd";
-      Size += strlen(Args[n-1]);
+
+#define ADDARG(X) Args.push_back(X); Size += strlen(X)
+#define ADDARGC(X) Args.push_back(X); Size += sizeof(X) - 1
+
+      ADDARGC("--status-fd");
+      char status_fd_buf[20];
       snprintf(status_fd_buf,sizeof(status_fd_buf),"%i", fd[1]);
       snprintf(status_fd_buf,sizeof(status_fd_buf),"%i", fd[1]);
-      Args[n++] = status_fd_buf;
-      Size += strlen(Args[n-1]);
-      
+      ADDARG(status_fd_buf);
+
       unsigned long const Op = I->Op;
       unsigned long const Op = I->Op;
 
 
       switch (I->Op)
       switch (I->Op)
       {
       {
 	 case Item::Remove:
 	 case Item::Remove:
-	 Args[n++] = "--force-depends";
-	 Size += strlen(Args[n-1]);
-	 Args[n++] = "--force-remove-essential";
-	 Size += strlen(Args[n-1]);
-	 Args[n++] = "--remove";
-	 Size += strlen(Args[n-1]);
+	 ADDARGC("--force-depends");
+	 ADDARGC("--force-remove-essential");
+	 ADDARGC("--remove");
 	 break;
 	 break;
 	 
 	 
 	 case Item::Purge:
 	 case Item::Purge:
-	 Args[n++] = "--force-depends";
-	 Size += strlen(Args[n-1]);
-	 Args[n++] = "--force-remove-essential";
-	 Size += strlen(Args[n-1]);
-	 Args[n++] = "--purge";
-	 Size += strlen(Args[n-1]);
+	 ADDARGC("--force-depends");
+	 ADDARGC("--force-remove-essential");
+	 ADDARGC("--purge");
 	 break;
 	 break;
 	 
 	 
 	 case Item::Configure:
 	 case Item::Configure:
-	 Args[n++] = "--configure";
-	 Size += strlen(Args[n-1]);
+	 ADDARGC("--configure");
 	 break;
 	 break;
 
 
 	 case Item::ConfigurePending:
 	 case Item::ConfigurePending:
-	 Args[n++] = "--configure";
-	 Size += strlen(Args[n-1]);
-	 Args[n++] = "--pending";
-	 Size += strlen(Args[n-1]);
+	 ADDARGC("--configure");
+	 ADDARGC("--pending");
 	 break;
 	 break;
 
 
 	 case Item::TriggersPending:
 	 case Item::TriggersPending:
-	 Args[n++] = "--triggers-only";
-	 Size += strlen(Args[n-1]);
-	 Args[n++] = "--pending";
-	 Size += strlen(Args[n-1]);
+	 ADDARGC("--triggers-only");
+	 ADDARGC("--pending");
 	 break;
 	 break;
 
 
 	 case Item::Install:
 	 case Item::Install:
-	 Args[n++] = "--unpack";
-	 Size += strlen(Args[n-1]);
-	 Args[n++] = "--auto-deconfigure";
-	 Size += strlen(Args[n-1]);
+	 ADDARGC("--unpack");
+	 ADDARGC("--auto-deconfigure");
 	 break;
 	 break;
       }
       }
 
 
       if (NoTriggers == true && I->Op != Item::TriggersPending &&
       if (NoTriggers == true && I->Op != Item::TriggersPending &&
 	  I->Op != Item::ConfigurePending)
 	  I->Op != Item::ConfigurePending)
       {
       {
-	 Args[n++] = "--no-triggers";
-	 Size += strlen(Args[n-1]);
+	 ADDARGC("--no-triggers");
       }
       }
+#undef ADDARGC
 
 
       // Write in the file or package names
       // Write in the file or package names
       if (I->Op == Item::Install)
       if (I->Op == Item::Install)
@@ -1035,10 +1028,10 @@ bool pkgDPkgPM::Go(int OutStatusFd)
 	 {
 	 {
 	    if (I->File[0] != '/')
 	    if (I->File[0] != '/')
 	       return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str());
 	       return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str());
-	    Args[n++] = I->File.c_str();
-	    Size += strlen(Args[n-1]);
+	    Args.push_back(I->File.c_str());
+	    Size += I->File.length();
 	 }
 	 }
-      }      
+      }
       else
       else
       {
       {
 	 string const nativeArch = _config->Find("APT::Architecture");
 	 string const nativeArch = _config->Find("APT::Architecture");
@@ -1049,30 +1042,36 @@ bool pkgDPkgPM::Go(int OutStatusFd)
 	       continue;
 	       continue;
 	    if (I->Op == Item::Configure && disappearedPkgs.find(I->Pkg.Name()) != disappearedPkgs.end())
 	    if (I->Op == Item::Configure && disappearedPkgs.find(I->Pkg.Name()) != disappearedPkgs.end())
 	       continue;
 	       continue;
-	    if (I->Pkg.Arch() == nativeArch || !strcmp(I->Pkg.Arch(), "all"))
-	       Args[n++] = I->Pkg.Name();
+	    if (false && (I->Pkg.Arch() == nativeArch || !strcmp(I->Pkg.Arch(), "all")))
+	    {
+	       char const * const name = I->Pkg.Name();
+	       ADDARG(name);
+	    }
 	    else
 	    else
 	    {
 	    {
-	       Packages[pkgcount] = strdup(I->Pkg.FullName(false).c_str());
-	       Args[n++] = Packages[pkgcount++];
+	       char * const fullname = strdup(I->Pkg.FullName(false).c_str());
+	       Packages.push_back(fullname);
+	       ADDARG(fullname);
 	    }
 	    }
-	    Size += strlen(Args[n-1]);
 	 }
 	 }
 	 // skip configure action if all sheduled packages disappeared
 	 // skip configure action if all sheduled packages disappeared
 	 if (oldSize == Size)
 	 if (oldSize == Size)
 	    continue;
 	    continue;
       }
       }
-      Args[n] = 0;
+#undef ADDARG
+
       J = I;
       J = I;
       
       
       if (_config->FindB("Debug::pkgDPkgPM",false) == true)
       if (_config->FindB("Debug::pkgDPkgPM",false) == true)
       {
       {
-	 for (unsigned int k = 0; k != n; k++)
-	    clog << Args[k] << ' ';
-	 clog << endl;
+	 for (std::vector<const char *>::const_iterator a = Args.begin();
+	      a != Args.end(); ++a)
+	    clog << *a << ' ';
+	 clog << "size=" << Size << endl;
 	 continue;
 	 continue;
       }
       }
-      
+      Args.push_back(NULL);
+
       cout << flush;
       cout << flush;
       clog << flush;
       clog << flush;
       cerr << flush;
       cerr << flush;
@@ -1186,7 +1185,7 @@ bool pkgDPkgPM::Go(int OutStatusFd)
 	 /* No Job Control Stop Env is a magic dpkg var that prevents it
 	 /* No Job Control Stop Env is a magic dpkg var that prevents it
 	    from using sigstop */
 	    from using sigstop */
 	 putenv((char *)"DPKG_NO_TSTP=yes");
 	 putenv((char *)"DPKG_NO_TSTP=yes");
-	 execvp(Args[0],(char **)Args);
+	 execvp(Args[0], (char**) &Args[0]);
 	 cerr << "Could not exec dpkg!" << endl;
 	 cerr << "Could not exec dpkg!" << endl;
 	 _exit(100);
 	 _exit(100);
       }      
       }      
@@ -1212,10 +1211,11 @@ bool pkgDPkgPM::Go(int OutStatusFd)
       sigemptyset(&sigmask);
       sigemptyset(&sigmask);
       sigprocmask(SIG_BLOCK,&sigmask,&original_sigmask);
       sigprocmask(SIG_BLOCK,&sigmask,&original_sigmask);
 
 
-      /* clean up the temporary allocation for multiarch package names in
-         the parent, so we don't leak memory when we return. */
-      for (unsigned int i = 0; i < pkgcount; i++)
-	 free(Packages[i]);
+      /* free vectors (and therefore memory) as we don't need the included data anymore */
+      for (std::vector<char *>::const_iterator p = Packages.begin();
+	   p != Packages.end(); ++p)
+	 free(*p);
+      Packages.clear();
 
 
       // the result of the waitpid call
       // the result of the waitpid call
       int res;
       int res;

+ 4 - 1
debian/changelog

@@ -4,8 +4,11 @@ apt (0.8.16~exp7) experimental; urgency=low
   * do not pollute namespace in the headers with using (Closes: #500198)
   * do not pollute namespace in the headers with using (Closes: #500198)
   * use forward declaration in headers if possible instead of includes
   * use forward declaration in headers if possible instead of includes
   * remove old APT_COMPATIBILITY ifdef's
   * remove old APT_COMPATIBILITY ifdef's
+  * apt-pkg/deb/dpkgpm.cc:
+    - use std::vector instead of fixed size arrays to store args and
+      multiarch-packagename strings
 
 
- -- David Kalnischkies <kalnischkies@gmail.com>  Mon, 19 Sep 2011 19:24:20 +0200
+ -- David Kalnischkies <kalnischkies@gmail.com>  Tue, 20 Sep 2011 11:32:36 +0200
 
 
 apt (0.8.16~exp6) experimental; urgency=low
 apt (0.8.16~exp6) experimental; urgency=low