mt_allocator.h (__mt_alloc::deallocate): Check for null pointer.
authorBenjamin Kosnik <bkoz@redhat.com>
Thu, 14 Oct 2004 23:03:26 +0000 (23:03 +0000)
committerBenjamin Kosnik <bkoz@gcc.gnu.org>
Thu, 14 Oct 2004 23:03:26 +0000 (23:03 +0000)
2004-10-14  Benjamin Kosnik  <bkoz@redhat.com>

* include/ext/mt_allocator.h (__mt_alloc::deallocate): Check for
null pointer.
* include/ext/pool_allocator.h (debug_allocator::deallocate):
Check pointer value.
* include/ext/debug_allocator.h (debug_allocator::deallocate):
Throw exceptions, don't abort.
* include/ext/array_allocator.h
(array_allocator_base::deallocate): Remove unused parameters.
* testsuite/testsuite_allocator.h (check_deallocate_null): New.
* testsuite/ext/mt_allocator/check_deallocate_null.cc: New.
* testsuite/ext/mt_allocator/check_deallocate_null_thread.cc: New.
* testsuite/ext/array_allocator/check_deallocate_null.cc: New.
* testsuite/ext/debug_allocator/check_deallocate_null.cc: New.
* testsuite/ext/malloc_allocator/check_deallocate_null.cc: New.
* testsuite/ext/new_allocator/check_deallocate_null.cc: New.
* testsuite/ext/pool_allocator/check_deallocate_null.cc: New.

* testsuite/testsuite_allocator.h (check_new): Add instance argument.
* testsuite/ext/array_allocator/check_new.cc: New.

From-SVN: r89060

14 files changed:
libstdc++-v3/ChangeLog
libstdc++-v3/include/ext/array_allocator.h
libstdc++-v3/include/ext/debug_allocator.h
libstdc++-v3/include/ext/mt_allocator.h
libstdc++-v3/include/ext/pool_allocator.h
libstdc++-v3/testsuite/ext/array_allocator/check_deallocate_null.cc [new file with mode: 0644]
libstdc++-v3/testsuite/ext/array_allocator/check_new.cc [new file with mode: 0644]
libstdc++-v3/testsuite/ext/debug_allocator/check_deallocate_null.cc [new file with mode: 0644]
libstdc++-v3/testsuite/ext/malloc_allocator/check_deallocate_null.cc [new file with mode: 0644]
libstdc++-v3/testsuite/ext/mt_allocator/check_deallocate_null.cc [new file with mode: 0644]
libstdc++-v3/testsuite/ext/mt_allocator/check_deallocate_null_thread.cc [new file with mode: 0644]
libstdc++-v3/testsuite/ext/new_allocator/check_deallocate_null.cc [new file with mode: 0644]
libstdc++-v3/testsuite/ext/pool_allocator/check_deallocate_null.cc [new file with mode: 0644]
libstdc++-v3/testsuite/testsuite_allocator.h

index d8652be25cba87b8ab335d0f43a1818f7e189c2a..d5bdab551b0603b181a472374305b02d990471b8 100644 (file)
@@ -1,3 +1,25 @@
+2004-10-14  Benjamin Kosnik  <bkoz@redhat.com>
+
+       * include/ext/mt_allocator.h (__mt_alloc::deallocate): Check for
+       null pointer.
+       * include/ext/pool_allocator.h (debug_allocator::deallocate):
+       Check pointer value.
+       * include/ext/debug_allocator.h (debug_allocator::deallocate):
+       Throw exceptions, don't abort.
+       * include/ext/array_allocator.h
+       (array_allocator_base::deallocate): Remove unused parameters.
+       * testsuite/testsuite_allocator.h (check_deallocate_null): New.
+       * testsuite/ext/mt_allocator/check_deallocate_null.cc: New.
+       * testsuite/ext/mt_allocator/check_deallocate_null_thread.cc: New.
+       * testsuite/ext/array_allocator/check_deallocate_null.cc: New.
+       * testsuite/ext/debug_allocator/check_deallocate_null.cc: New.
+       * testsuite/ext/malloc_allocator/check_deallocate_null.cc: New.
+       * testsuite/ext/new_allocator/check_deallocate_null.cc: New.
+       * testsuite/ext/pool_allocator/check_deallocate_null.cc: New.
+
+       * testsuite/testsuite_allocator.h (check_new): Add instance argument.
+       * testsuite/ext/array_allocator/check_new.cc: New.
+       
 2004-10-14  Paolo Carlini  <pcarlini@suse.de>
 
        * include/ext/bitmap_allocator.h (bitmap_allocator::_Alloc_block):
index 66aa903e1540725f7c279305dcee289ebcb893d1..585570a5722bd2e8f72eaa5429f5a510c04681d4 100644 (file)
@@ -55,7 +55,7 @@ namespace __gnu_cxx
       address(const_reference __x) const { return &__x; }
 
       void
-      deallocate(pointer __p, size_type)
+      deallocate(pointer, size_type)
       { 
        // Does nothing.
       }
index 7ea6fb42f9808d9ee996878ec1507442577345e7..7fff5d389d88be6d1735260dbb1c18c41a126eda 100644 (file)
@@ -48,7 +48,7 @@
 #ifndef _DEBUG_ALLOCATOR_H
 #define _DEBUG_ALLOCATOR_H 1
 
-#include <cstdlib>
+#include <stdexcept>
 
 namespace __gnu_cxx
 {
@@ -108,12 +108,18 @@ namespace __gnu_cxx
       void
       deallocate(pointer __p, size_type __n)
       {
-       if (!__p)
-         abort();
-       pointer __real_p = __p - _M_extra;
-        if (*reinterpret_cast<size_type*>(__real_p) != __n)
-          abort();
-        _M_allocator.deallocate(__real_p, __n + _M_extra);
+       if (__p)
+         {
+           pointer __real_p = __p - _M_extra;
+           if (*reinterpret_cast<size_type*>(__real_p) != __n)
+             {
+               throw std::runtime_error("debug_allocator::deallocate"
+                                        " wrong size");
+             }
+           _M_allocator.deallocate(__real_p, __n + _M_extra);
+         }
+       else
+         throw std::runtime_error("debug_allocator::deallocate null pointer");
       }
     };
 } // namespace __gnu_cxx
index 3bbfc79b4bb5ad995c7065856f770f3a03afd6e0..d4d51d8a55ecd9c1751802a1142f379ce59c0696 100644 (file)
@@ -724,14 +724,17 @@ namespace __gnu_cxx
     __mt_alloc<_Tp, _Poolp>::
     deallocate(pointer __p, size_type __n)
     {
-      // Requests larger than _M_max_bytes are handled by operators
-      // new/delete directly.
-      __pool_type& __pool = this->_S_get_pool();
-      const size_t __bytes = __n * sizeof(_Tp);
-      if (__pool._M_check_threshold(__bytes))
-       ::operator delete(__p);
-      else
-       __pool._M_reclaim_block(reinterpret_cast<char*>(__p), __bytes);
+      if (__builtin_expect(__p != 0, true))
+       {
+         // Requests larger than _M_max_bytes are handled by
+         // operators new/delete directly.
+         __pool_type& __pool = this->_S_get_pool();
+         const size_t __bytes = __n * sizeof(_Tp);
+         if (__pool._M_check_threshold(__bytes))
+           ::operator delete(__p);
+         else
+           __pool._M_reclaim_block(reinterpret_cast<char*>(__p), __bytes);
+       }
     }
   
   template<typename _Tp, typename _Poolp>
index 0f087a03c1e90968319d3b4c15bccbb606e32097..0b95906ccfd1b93a4bc78d2d21ca5735b5b81b1d 100644 (file)
@@ -234,7 +234,7 @@ namespace __gnu_cxx
     void
     __pool_alloc<_Tp>::deallocate(pointer __p, size_type __n)
     {
-      if (__n)
+      if (__n && (__p != 0))
        {
          const size_t __bytes = __n * sizeof(_Tp);
          if (__bytes > static_cast<size_t>(_S_max_bytes) || _S_force_new == 1)
diff --git a/libstdc++-v3/testsuite/ext/array_allocator/check_deallocate_null.cc b/libstdc++-v3/testsuite/ext/array_allocator/check_deallocate_null.cc
new file mode 100644 (file)
index 0000000..9d743a1
--- /dev/null
@@ -0,0 +1,32 @@
+//
+// Copyright (C) 2004 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 20.4.1.1 allocator members
+
+#include <ext/array_allocator.h>
+#include <testsuite_allocator.h>
+
+int main()
+{ 
+  typedef int value_type;
+  typedef __gnu_cxx::array_allocator<value_type> allocator_type;
+  __gnu_test::check_deallocate_null<allocator_type>(); 
+  return 0;
+}
+
diff --git a/libstdc++-v3/testsuite/ext/array_allocator/check_new.cc b/libstdc++-v3/testsuite/ext/array_allocator/check_new.cc
new file mode 100644 (file)
index 0000000..2d15972
--- /dev/null
@@ -0,0 +1,59 @@
+// 2001-11-25  Phil Edwards  <pme@gcc.gnu.org>
+//
+// Copyright (C) 2001, 2003, 2004 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 20.4.1.1 allocator members
+
+#include <cstdlib>
+#include <ext/array_allocator.h>
+#include <testsuite_allocator.h>
+
+using __gnu_cxx::array_allocator;
+
+void* 
+operator new(std::size_t n) throw(std::bad_alloc)
+{
+  new_called = true;
+  requested = n;
+  return std::malloc(n);
+}
+
+void
+operator delete(void *v) throw()
+{
+  delete_called = true;
+  return std::free(v);
+}
+
+// These just help tracking down error messages.
+bool test01() 
+{ 
+  typedef unsigned int value_type;
+  typedef std::tr1::array<value_type, 15> array_type;
+  typedef array_allocator<value_type, array_type> allocator_type;
+  array_type store;
+  allocator_type a(&store);
+  return (__gnu_test::check_new<allocator_type, false>(a) == false); 
+}
+
+int main()
+{
+  return test01();
+}
+
diff --git a/libstdc++-v3/testsuite/ext/debug_allocator/check_deallocate_null.cc b/libstdc++-v3/testsuite/ext/debug_allocator/check_deallocate_null.cc
new file mode 100644 (file)
index 0000000..dd9d281
--- /dev/null
@@ -0,0 +1,48 @@
+//
+// Copyright (C) 2004 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 20.4.1.1 allocator members
+
+#include <memory>
+#include <ext/debug_allocator.h>
+#include <testsuite_allocator.h>
+#include <testsuite_hooks.h>
+
+int main()
+{ 
+  typedef int value_type;
+  typedef std::allocator<value_type> debug_type;
+  typedef __gnu_cxx::debug_allocator<debug_type> allocator_type;
+
+  try
+    {
+      __gnu_test::check_deallocate_null<allocator_type>(); 
+    }
+  catch (std::runtime_error& obj)
+    {
+      // Ok.
+    }
+  catch (...)
+    { 
+      // Shouldn't get here.
+      throw;
+    }
+  return 0;
+}
+
diff --git a/libstdc++-v3/testsuite/ext/malloc_allocator/check_deallocate_null.cc b/libstdc++-v3/testsuite/ext/malloc_allocator/check_deallocate_null.cc
new file mode 100644 (file)
index 0000000..bdea99b
--- /dev/null
@@ -0,0 +1,32 @@
+//
+// Copyright (C) 2004 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 20.4.1.1 allocator members
+
+#include <ext/malloc_allocator.h>
+#include <testsuite_allocator.h>
+
+int main()
+{ 
+  typedef int value_type;
+  typedef __gnu_cxx::malloc_allocator<value_type> allocator_type;
+  __gnu_test::check_deallocate_null<allocator_type>(); 
+  return 0;
+}
+
diff --git a/libstdc++-v3/testsuite/ext/mt_allocator/check_deallocate_null.cc b/libstdc++-v3/testsuite/ext/mt_allocator/check_deallocate_null.cc
new file mode 100644 (file)
index 0000000..d9cdb0f
--- /dev/null
@@ -0,0 +1,33 @@
+//
+// Copyright (C) 2004 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 20.4.1.1 allocator members
+
+#include <ext/mt_allocator.h>
+#include <testsuite_allocator.h>
+
+int main()
+{ 
+  typedef int value_type;
+  typedef __gnu_cxx::__common_pool_policy<false> policy_type;
+  typedef __gnu_cxx::__mt_alloc<value_type, policy_type> allocator_type;
+  __gnu_test::check_deallocate_null<allocator_type>(); 
+  return 0;
+}
+
diff --git a/libstdc++-v3/testsuite/ext/mt_allocator/check_deallocate_null_thread.cc b/libstdc++-v3/testsuite/ext/mt_allocator/check_deallocate_null_thread.cc
new file mode 100644 (file)
index 0000000..28aed3b
--- /dev/null
@@ -0,0 +1,33 @@
+//
+// Copyright (C) 2004 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 20.4.1.1 allocator members
+
+#include <ext/mt_allocator.h>
+#include <testsuite_allocator.h>
+
+int main()
+{ 
+  typedef int value_type;
+  typedef __gnu_cxx::__common_pool_policy<true> policy_type;
+  typedef __gnu_cxx::__mt_alloc<value_type, policy_type> allocator_type;
+  __gnu_test::check_deallocate_null<allocator_type>(); 
+  return 0;
+}
+
diff --git a/libstdc++-v3/testsuite/ext/new_allocator/check_deallocate_null.cc b/libstdc++-v3/testsuite/ext/new_allocator/check_deallocate_null.cc
new file mode 100644 (file)
index 0000000..34ed42c
--- /dev/null
@@ -0,0 +1,32 @@
+//
+// Copyright (C) 2004 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 20.4.1.1 allocator members
+
+#include <ext/new_allocator.h>
+#include <testsuite_allocator.h>
+
+int main()
+{ 
+  typedef int value_type;
+  typedef __gnu_cxx::new_allocator<value_type> allocator_type;
+  __gnu_test::check_deallocate_null<allocator_type>(); 
+  return 0;
+}
+
diff --git a/libstdc++-v3/testsuite/ext/pool_allocator/check_deallocate_null.cc b/libstdc++-v3/testsuite/ext/pool_allocator/check_deallocate_null.cc
new file mode 100644 (file)
index 0000000..4b4999e
--- /dev/null
@@ -0,0 +1,32 @@
+//
+// Copyright (C) 2004 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 20.4.1.1 allocator members
+
+#include <ext/pool_allocator.h>
+#include <testsuite_allocator.h>
+
+int main()
+{ 
+  typedef int value_type;
+  typedef __gnu_cxx::__pool_alloc<value_type> allocator_type;
+  __gnu_test::check_deallocate_null<allocator_type>(); 
+  return 0;
+}
+
index 7d373aa08f9760b532d019006da0815f7191f2c1..ecb210e1fde87b70d3cac307168c1653cb446cd2 100644 (file)
@@ -1,7 +1,7 @@
 // -*- C++ -*-
 // Testing allocator for the C++ library testsuite.
 //
-// Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+// Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
@@ -181,20 +181,29 @@ namespace __gnu_test
   check_construct_destroy(const char* tag, int expected_c, int expected_d);
 
   template<typename Alloc, bool uses_global_new_and_delete>
-  bool check_new()
-  {
-    bool test __attribute__((unused)) = true;
-    Alloc  a;
-    typename Alloc::pointer p = a.allocate(10);
-    if (uses_global_new_and_delete)  
-      test &= ( requested >= (10 * 15 * sizeof(long)) );
-
-    test &= ( new_called == uses_global_new_and_delete );
-    a.deallocate(p, 10);
-    test &= ( delete_called == uses_global_new_and_delete );
-  
-    return test;
-  }
+    bool 
+    check_new(Alloc a = Alloc())
+    {
+      bool test __attribute__((unused)) = true;
+      typename Alloc::pointer p = a.allocate(10);
+      if (uses_global_new_and_delete)  
+       test &= ( requested >= (10 * 15 * sizeof(long)) );
+      
+      test &= ( new_called == uses_global_new_and_delete );
+      a.deallocate(p, 10);
+      test &= ( delete_called == uses_global_new_and_delete );
+      
+      return test;
+    }
+
+  template<typename Alloc>
+    bool 
+    check_deallocate_null()
+    {
+      // Let's not core here...
+      Alloc  a;
+      a.deallocate(NULL, 10);
+    }
 }; // namespace __gnu_test
 
 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H