- Fix the Fallback option to work correctly, by not calling
    realloc() on a map mapped by mmap(), and by using malloc
    and friends instead of new[].
    {
       if ((Flags & ReadOnly) != ReadOnly)
         return _error->Error("Compressed file %s can only be mapped readonly", Fd.Name().c_str());
    {
       if ((Flags & ReadOnly) != ReadOnly)
         return _error->Error("Compressed file %s can only be mapped readonly", Fd.Name().c_str());
-      Base = new unsigned char[iSize];
       SyncToFd = new FileFd();
       if (Fd.Seek(0L) == false || Fd.Read(Base, iSize) == false)
         return _error->Error("Compressed file %s can't be read into mmap", Fd.Name().c_str());
       SyncToFd = new FileFd();
       if (Fd.Seek(0L) == false || Fd.Read(Base, iSize) == false)
         return _error->Error("Compressed file %s can't be read into mmap", Fd.Name().c_str());
-   Base = mmap(0,iSize,Prot,Map,Fd.Fd(),0);
+   Base = (Flags & Fallback) ? MAP_FAILED : mmap(0,iSize,Prot,Map,Fd.Fd(),0);
    if (Base == (void *)-1)
    {
    if (Base == (void *)-1)
    {
-      if (errno == ENODEV || errno == EINVAL)
+      if (errno == ENODEV || errno == EINVAL || (Flags & Fallback))
       {
         // The filesystem doesn't support this particular kind of mmap.
         // So we allocate a buffer and read the whole file into it.
         if ((Flags & ReadOnly) == ReadOnly)
         {
            // for readonly, we don't need sync, so make it simple
       {
         // The filesystem doesn't support this particular kind of mmap.
         // So we allocate a buffer and read the whole file into it.
         if ((Flags & ReadOnly) == ReadOnly)
         {
            // for readonly, we don't need sync, so make it simple
-           Base = new unsigned char[iSize];
            return Fd.Read(Base, iSize);
         }
         // FIXME: Writing to compressed fd's ?
            return Fd.Read(Base, iSize);
         }
         // FIXME: Writing to compressed fd's ?
         if (dupped_fd == -1)
            return _error->Errno("mmap", _("Couldn't duplicate file descriptor %i"), Fd.Fd());
 
         if (dupped_fd == -1)
            return _error->Errno("mmap", _("Couldn't duplicate file descriptor %i"), Fd.Fd());
 
-        Base = new unsigned char[iSize];
+        Base = calloc(iSize, 1);
         SyncToFd = new FileFd (dupped_fd);
         if (!SyncToFd->Seek(0L) || !SyncToFd->Read(Base, iSize))
            return false;
         SyncToFd = new FileFd (dupped_fd);
         if (!SyncToFd->Seek(0L) || !SyncToFd->Read(Base, iSize))
            return false;
       delete SyncToFd;
       SyncToFd = NULL;
    }
       delete SyncToFd;
       SyncToFd = NULL;
    }
        }
 #endif
        // fallback to a static allocated space
        }
 #endif
        // fallback to a static allocated space
-       Base = new unsigned char[WorkSpace];
-       memset(Base,0,WorkSpace);
+       Base = calloc(WorkSpace, 1);
 #ifdef _POSIX_MAPPED_FILES
       munmap(Base, WorkSpace);
 #else
 #ifdef _POSIX_MAPPED_FILES
       munmap(Base, WorkSpace);
 #else
-      delete [] (unsigned char *)Base;
 
+apt (0.9.7) UNRELEASED; urgency=low
+
+  * apt-pkg/contrib/mmap.cc:
+    - Fix the Fallback option to work correctly, by not calling
+      realloc() on a map mapped by mmap(), and by using malloc
+      and friends instead of new[].
+
+ -- Julian Andres Klode <jak@debian.org>  Sat, 16 Jun 2012 14:28:38 +0200
+
 apt (0.9.6) unstable; urgency=low
 
   [ David Kalnischkies ]
 apt (0.9.6) unstable; urgency=low
 
   [ David Kalnischkies ]