]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/mmap.cc
It is NOT OK to just munmap memory from malloc :/.
[apt.git] / apt-pkg / contrib / mmap.cc
index f632cd6c54ecc622b385a63961f08700b71fe5e8..f63f2eea1a9c6080d3df07c0a7e22002603e1c96 100644 (file)
@@ -106,14 +106,14 @@ bool MMap::Map(FileFd &Fd)
            if (unlikely(Base == nullptr))
               return _error->Errno("MMap-malloc", _("Couldn't make mmap of %llu bytes"), iSize);
            SyncToFd = new FileFd();
-           return Fd.Read(Base, iSize);
+           return Fd.Seek(0L) && Fd.Read(Base, iSize);
         }
         // FIXME: Writing to compressed fd's ?
         int const dupped_fd = dup(Fd.Fd());
         if (dupped_fd == -1)
            return _error->Errno("mmap", _("Couldn't duplicate file descriptor %i"), Fd.Fd());
 
-        Base = calloc(iSize, 1);
+        Base = malloc(iSize);
         if (unlikely(Base == nullptr))
            return _error->Errno("MMap-calloc", _("Couldn't make mmap of %llu bytes"), iSize);
         SyncToFd = new FileFd (dupped_fd);
@@ -307,10 +307,11 @@ DynamicMMap::~DynamicMMap()
       if (validData() == false)
         return;
 #ifdef _POSIX_MAPPED_FILES
-      munmap(Base, WorkSpace);
-#else
-      free(Base);
+      if ((Flags & Fallback) != Fallback) {
+         munmap(Base, WorkSpace);
+      } else
 #endif
+      free(Base);
       return;
    }
    
@@ -489,12 +490,14 @@ bool DynamicMMap::Grow() {
                if ((Flags & Moveable) != Moveable)
                        return false;
 
-               Base = realloc(Base, newSize);
-               if (Base == NULL)
+               auto Temp = realloc(Base, newSize);
+               if (Temp == NULL)
                        return false;
-               else
+               else {
+                       Base = Temp;
                        /* Set new memory to 0 */
                        memset((char*)Base + WorkSpace, 0, newSize - WorkSpace);
+               }
        }
 
        Pools =(Pool*) Base + poolOffset;