[GRASS-SVN] r71625 - in grass/trunk: include/iostream lib/iostream

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Nov 2 06:14:12 PDT 2017


Author: mmetz
Date: 2017-11-02 06:14:12 -0700 (Thu, 02 Nov 2017)
New Revision: 71625

Modified:
   grass/trunk/include/iostream/mm.h
   grass/trunk/lib/iostream/mm.cpp
Log:
iostreamlib: fix memory manager

Modified: grass/trunk/include/iostream/mm.h
===================================================================
--- grass/trunk/include/iostream/mm.h	2017-11-02 10:53:40 UTC (rev 71624)
+++ grass/trunk/include/iostream/mm.h	2017-11-02 13:14:12 UTC (rev 71625)
@@ -132,18 +132,20 @@
  
   void print();
 
-  friend class mm_register_init;
+  // make these members of MM_register
 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
-  friend void * operator new(size_t) throw (std::bad_alloc);
-  friend void * operator new[] (size_t) throw (std::bad_alloc);
-  friend void operator delete(void *) throw();
-  friend void operator delete[](void *) throw();
+  void * operator new(size_t) throw (std::bad_alloc);
+  void * operator new[] (size_t) throw (std::bad_alloc);
+  void operator delete(void *) throw();
+  void operator delete[](void *) throw();
 #else
-  friend void * operator new(size_t);
-  friend void * operator new[] (size_t);
-  friend void operator delete(void *) noexcept;
-  friend void operator delete[](void *) noexcept;
+  void * operator new(size_t);
+  void * operator new[] (size_t);
+  void operator delete(void *) noexcept;
+  void operator delete[](void *) noexcept;
 #endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
+
+  friend class mm_register_init;
 };
 
 

Modified: grass/trunk/lib/iostream/mm.cpp
===================================================================
--- grass/trunk/lib/iostream/mm.cpp	2017-11-02 10:53:40 UTC (rev 71624)
+++ grass/trunk/lib/iostream/mm.cpp	2017-11-02 13:14:12 UTC (rev 71625)
@@ -276,13 +276,18 @@
 
  
 /* ************************************************************ */
+/* these overloaded operators must only be used by this memory manager
+ * risk of invalid free if these operators are defined outside the MM_register class
+ * e.g. GDAL allocating memory with something else than new as defined here
+ * but then using delete as defined here 
+ */
 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
-void* operator new[] (size_t sz) throw (std::bad_alloc) {
+void* MM_register::operator new[] (size_t sz) throw (std::bad_alloc) {
 #else
-void* operator new[] (size_t sz) {
+void* MM_register::operator new[] (size_t sz) {
 #endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
   void *p;
-  
+
   MM_DEBUG cout << "new: sz=" << sz << ", register " 
 		<< sz+SIZE_SPACE << "B ,"; 
 
@@ -320,7 +325,7 @@
     assert(0);
     exit (1);
   }
-  
+
   *((size_t *) p) = sz;
   
   MM_DEBUG cout << "ptr=" << (void*) (((char *) p) + SIZE_SPACE) << endl;
@@ -332,12 +337,12 @@
  
 /* ************************************************************ */
 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
-void* operator new (size_t sz) throw (std::bad_alloc) {
+void* MM_register::operator new (size_t sz) throw (std::bad_alloc) {
 #else
-void* operator new (size_t sz) {
+void* MM_register::operator new (size_t sz) {
 #endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
   void *p;
-  
+
   MM_DEBUG cout << "new: sz=" << sz << ", register " 
 		<< sz+SIZE_SPACE << "B ,"; 
 
@@ -375,7 +380,7 @@
     assert(0);
     exit (1);
   }
-  
+
   *((size_t *) p) = sz;
   
   MM_DEBUG cout << "ptr=" << (void*) (((char *) p) + SIZE_SPACE) << endl;
@@ -388,13 +393,13 @@
 
 /* ---------------------------------------------------------------------- */
 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
-void operator delete (void *ptr) throw() {
+void MM_register::operator delete (void *ptr) throw() {
 #else
-void operator delete (void *ptr) noexcept {
+void MM_register::operator delete (void *ptr) noexcept {
 #endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
   size_t sz;
   void *p;
-  
+
   MM_DEBUG cout << "delete: ptr=" << ptr << ","; 
 
   if (!ptr) {
@@ -411,6 +416,9 @@
   }
   
   assert(ptr);
+
+  /* causes invalid free if ptr has not been allocated with new as 
+   * defined above */
   p = ((char *)ptr) - SIZE_SPACE; // the base of memory
   sz = *((size_t *)p);
   
@@ -432,9 +440,9 @@
 
 /* ---------------------------------------------------------------------- */
 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
-void operator delete[] (void *ptr) throw() {
+void MM_register::operator delete[] (void *ptr) throw() {
 #else
-void operator delete[] (void *ptr) noexcept {
+void MM_register::operator delete[] (void *ptr) noexcept {
 #endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
   size_t sz;
   void *p;
@@ -450,6 +458,9 @@
     return;
   }
    assert(ptr);
+
+  /* causes invalid free if ptr has not been allocated with new as 
+   * defined above */
    p = ((char *)ptr) - SIZE_SPACE; // the base of memory
    sz = *((size_t *)p);
 



More information about the grass-commit mailing list