From 6bae2c5108a95267fedcc8f2312e91488ebece8d Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 21 May 2012 19:12:25 +0200 Subject: =?UTF-8?q?*=20apt-pkg/contrib/mmap.cc:=20=20=20-=20have=20a=20dum?= =?UTF-8?q?my=20SyncToFd=20around=20in=20case=20of=20ReadOnly=20access=20t?= =?UTF-8?q?o=20a=20=20=20=20=20compressed=20file=20as=20we=20otherwise=20o?= =?UTF-8?q?n=20Close()=20do=20not=20delete[]=20the=20=20=20=20=20char=20bu?= =?UTF-8?q?ffer=20but=20munmap()=20it=E2=80=A6=20(Closes:=20#673815)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apt-pkg/contrib/mmap.cc | 1 + 1 file changed, 1 insertion(+) (limited to 'apt-pkg/contrib/mmap.cc') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 160718ea5..2d12b6fe9 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -84,6 +84,7 @@ bool MMap::Map(FileFd &Fd) 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()); return true; -- cgit v1.2.3 From 7899a1339ced1e8fa10730e5619349b9fd782fac Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Sat, 16 Jun 2012 14:35:15 +0200 Subject: * 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[]. --- apt-pkg/contrib/mmap.cc | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'apt-pkg/contrib/mmap.cc') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 2d12b6fe9..131cefd6e 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -83,7 +83,7 @@ bool MMap::Map(FileFd &Fd) { if ((Flags & ReadOnly) != ReadOnly) return _error->Error("Compressed file %s can only be mapped readonly", Fd.Name().c_str()); - Base = new unsigned char[iSize]; + Base = malloc(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()); @@ -91,17 +91,17 @@ bool MMap::Map(FileFd &Fd) } // Map it. - 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 (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 - Base = new unsigned char[iSize]; + Base = malloc(iSize); return Fd.Read(Base, iSize); } // FIXME: Writing to compressed fd's ? @@ -109,7 +109,7 @@ bool MMap::Map(FileFd &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; @@ -135,7 +135,7 @@ bool MMap::Close(bool DoSync) if (SyncToFd != NULL) { - delete[] (char *)Base; + free(Base); delete SyncToFd; SyncToFd = NULL; } @@ -283,8 +283,7 @@ DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace, } #endif // fallback to a static allocated space - Base = new unsigned char[WorkSpace]; - memset(Base,0,WorkSpace); + Base = calloc(WorkSpace, 1); iSize = 0; } /*}}}*/ @@ -300,7 +299,7 @@ DynamicMMap::~DynamicMMap() #ifdef _POSIX_MAPPED_FILES munmap(Base, WorkSpace); #else - delete [] (unsigned char *)Base; + free(Base); #endif return; } -- cgit v1.2.3 From 61a406f0fc9c7b1807acb2ff146db6acd18e879c Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Sat, 16 Jun 2012 14:38:09 +0200 Subject: Zero out the new memory allocated with realloc(). --- apt-pkg/contrib/mmap.cc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'apt-pkg/contrib/mmap.cc') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 131cefd6e..77b4502f5 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -461,6 +461,8 @@ bool DynamicMMap::Grow() { return false; Base = realloc(Base, newSize); + /* Set new memory to 0 */ + memset((char*)Base + WorkSpace, 0, newSize - WorkSpace); if (Base == NULL) return false; } -- cgit v1.2.3 From e3026ce49a0839dfbe494af7e60d91bc7204f88e Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Sat, 16 Jun 2012 15:23:06 +0200 Subject: Fix previous commit: Do not call memset() if realloc() returned NULL. --- apt-pkg/contrib/mmap.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'apt-pkg/contrib/mmap.cc') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 77b4502f5..593bb063b 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -461,10 +461,11 @@ bool DynamicMMap::Grow() { return false; Base = realloc(Base, newSize); - /* Set new memory to 0 */ - memset((char*)Base + WorkSpace, 0, newSize - WorkSpace); if (Base == NULL) return false; + else + /* Set new memory to 0 */ + memset((char*)Base + WorkSpace, 0, newSize - WorkSpace); } Pools =(Pool*) Base + poolOffset; -- cgit v1.2.3