]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/fileutl.cc
fix double free (closes: #711045)
[apt.git] / apt-pkg / contrib / fileutl.cc
index c7f78cdfba787fecd21251c3fd3f7672c69cba05..46661887a1173c959a45fa466bb5f689e9afc761 100644 (file)
@@ -387,7 +387,7 @@ std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> c
 
    std::vector<string> List;
 
-   if (DirectoryExists(Dir.c_str()) == false)
+   if (DirectoryExists(Dir) == false)
    {
       _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str());
       return List;
@@ -413,14 +413,14 @@ std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> c
       if (Ent->d_type != DT_REG)
 #endif
       {
-        if (RealFileExists(File.c_str()) == false)
+        if (RealFileExists(File) == false)
         {
            // do not show ignoration warnings for directories
            if (
 #ifdef _DIRENT_HAVE_D_TYPE
                Ent->d_type == DT_DIR ||
 #endif
-               DirectoryExists(File.c_str()) == true)
+               DirectoryExists(File) == true)
               continue;
            if (SilentIgnore.Match(Ent->d_name) == false)
               _error->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent->d_name, Dir.c_str());
@@ -501,7 +501,7 @@ std::vector<string> GetListOfFilesInDir(string const &Dir, bool SortList)
 
    std::vector<string> List;
 
-   if (DirectoryExists(Dir.c_str()) == false)
+   if (DirectoryExists(Dir) == false)
    {
       _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str());
       return List;
@@ -526,7 +526,7 @@ std::vector<string> GetListOfFilesInDir(string const &Dir, bool SortList)
       if (Ent->d_type != DT_REG)
 #endif
       {
-        if (RealFileExists(File.c_str()) == false)
+        if (RealFileExists(File) == false)
         {
            if (Debug == true)
               std::clog << "Bad file: " << Ent->d_name << " → it is not a real file" << std::endl;
@@ -1024,7 +1024,20 @@ bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration:
 {
    Close();
    Flags = (AutoClose) ? FileFd::AutoClose : 0;
-   iFd = Fd;
+   if (AutoClose == false && (
+#ifdef HAVE_ZLIB
+       compressor.Name == "gzip" ||
+#endif
+#ifdef HAVE_BZ2
+       compressor.Name == "bzip2" ||
+#endif
+       false))
+   {
+      // Need to duplicate fd here or gzclose for cleanup will close the fd as well
+      iFd = dup(Fd);
+   }
+   else
+      iFd = Fd;
    this->FileName = "";
    if (OpenInternDescriptor(Mode, compressor) == false)
    {
@@ -1183,8 +1196,6 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C
       close(Pipe[0]);
    else
       close(Pipe[1]);
-   if ((Comp == true || FileName.empty() == true) && d->compressed_fd != -1)
-      close(d->compressed_fd);
 
    return true;
 }
@@ -1413,8 +1424,11 @@ bool FileFd::Seek(unsigned long long To)
         return _error->Error("Reopen is only implemented for read-only files!");
       }
 #ifdef HAVE_BZ2
-      if (d->bz2 != NULL)
-        BZ2_bzclose(d->bz2);
+     if (d->bz2 != NULL) 
+     {
+       BZ2_bzclose(d->bz2);
+       d->bz2 = NULL;
+     }
 #endif
       if (iFd != -1)
         close(iFd);
@@ -1712,7 +1726,7 @@ bool FileFd::Close()
       }
    }
 
-   if ((Flags & Replace) == Replace && iFd >= 0) {
+   if ((Flags & Replace) == Replace) {
       if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0)
         Res &= _error->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName.c_str(), FileName.c_str());
 
@@ -1737,13 +1751,11 @@ bool FileFd::Close()
 /* */
 bool FileFd::Sync()
 {
-#ifdef _POSIX_SYNCHRONIZED_IO
    if (fsync(iFd) != 0)
    {
       Flags |= Fail;
       return _error->Errno("sync",_("Problem syncing the file"));
    }
-#endif
    return true;
 }
                                                                        /*}}}*/