Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r78317 - in sandbox/pool: boost/pool libs/pool/test
From: svart.riddare_at_[hidden]
Date: 2012-05-03 14:53:11


Author: edupuis
Date: 2012-05-03 14:53:09 EDT (Thu, 03 May 2012)
New Revision: 78317
URL: http://svn.boost.org/trac/boost/changeset/78317

Log:
Pool no longer crashes when freeing a null pointer. This is somewhat like the free() function and the delete operator and fixes #6561.
Added:
   sandbox/pool/libs/pool/test/test_bug_6561.cpp (contents, props changed)
Text files modified:
   sandbox/pool/boost/pool/object_pool.hpp | 7 +++++--
   sandbox/pool/boost/pool/pool.hpp | 22 ++++++++++++----------
   sandbox/pool/boost/pool/static_pool.hpp | 5 -----
   3 files changed, 17 insertions(+), 17 deletions(-)

Modified: sandbox/pool/boost/pool/object_pool.hpp
==============================================================================
--- sandbox/pool/boost/pool/object_pool.hpp (original)
+++ sandbox/pool/boost/pool/object_pool.hpp 2012-05-03 14:53:09 EDT (Thu, 03 May 2012)
@@ -190,8 +190,11 @@
       //! p->~ElementType(); this->free(p);
       //!
       //! \pre p must have been previously allocated from *this via a call to \ref construct.
- chunk->~T();
- (free)(chunk);
+ if (chunk)
+ {
+ chunk->~T();
+ (free)(chunk);
+ }
     }
 };
 

Modified: sandbox/pool/boost/pool/pool.hpp
==============================================================================
--- sandbox/pool/boost/pool/pool.hpp (original)
+++ sandbox/pool/boost/pool/pool.hpp 2012-05-03 14:53:09 EDT (Thu, 03 May 2012)
@@ -453,14 +453,14 @@
     // pre: 'chunk' must have been previously
     // returned by *this.malloc().
     void free BOOST_PREVENT_MACRO_SUBSTITUTION(void * const chunk)
- { //! Deallocates a chunk of memory. Note that chunk may not be 0. O(1).
+ { //! Deallocates a chunk of memory. O(1).
       //!
       //! Chunk must have been previously returned by t.malloc() or t.ordered_malloc().
       //! Assumes that chunk actually refers to a block of chunks
       //! spanning n * partition_sz bytes.
       //! deallocates each chunk in that block.
- //! Note that chunk may not be 0. O(n).
- (store().free)(chunk);
+ if (chunk)
+ (store().free)(chunk);
     }
 
     // pre: 'chunk' must have been previously
@@ -468,9 +468,10 @@
     void ordered_free(void * const chunk)
     { //! Same as above, but is order-preserving.
       //!
- //! Note that chunk may not be 0. O(N) with respect to the size of the free list.
+ //! O(N) with respect to the size of the free list.
       //! chunk must have been previously returned by t.malloc() or t.ordered_malloc().
- store().ordered_free(chunk);
+ if (chunk)
+ store().ordered_free(chunk);
     }
 
     // pre: 'chunk' must have been previously
@@ -480,14 +481,14 @@
       //!
       //! chunk must have been previously returned by t.ordered_malloc(n)
       //! spanning n * partition_sz bytes.
- //! Deallocates each chunk in that block.
- //! Note that chunk may not be 0. O(n).
+ //! Deallocates each chunk in that block. O(n).
       const size_type partition_size = alloc_size();
       const size_type total_req_size = n * requested_size;
       const size_type num_chunks = total_req_size / partition_size +
           ((total_req_size % partition_size) ? true : false);
 
- store().free_n(chunks, num_chunks, partition_size);
+ if (chunks)
+ store().free_n(chunks, num_chunks, partition_size);
     }
 
     // pre: 'chunk' must have been previously
@@ -496,7 +497,7 @@
     { //! Assumes that chunk actually refers to a block of chunks spanning n * partition_sz bytes;
       //! deallocates each chunk in that block.
       //!
- //! Note that chunk may not be 0. Order-preserving. O(N + n) where N is the size of the free list.
+ //! Order-preserving. O(N + n) where N is the size of the free list.
       //! chunk must have been previously returned by t.malloc() or t.ordered_malloc().
 
       const size_type partition_size = alloc_size();
@@ -504,7 +505,8 @@
       const size_type num_chunks = total_req_size / partition_size +
           ((total_req_size % partition_size) ? true : false);
 
- store().ordered_free_n(chunks, num_chunks, partition_size);
+ if (chunks)
+ store().ordered_free_n(chunks, num_chunks, partition_size);
     }
 
     // is_from() tests a chunk to determine if it was allocated from *this

Modified: sandbox/pool/boost/pool/static_pool.hpp
==============================================================================
--- sandbox/pool/boost/pool/static_pool.hpp (original)
+++ sandbox/pool/boost/pool/static_pool.hpp 2012-05-03 14:53:09 EDT (Thu, 03 May 2012)
@@ -39,12 +39,7 @@
     :
     pool<UserAllocator>(nrequested_size, nrequested_items)
     {
-#if 1
- void *p = pool<UserAllocator>::malloc BOOST_PREVENT_MACRO_SUBSTITUTION();
- p ? free BOOST_PREVENT_MACRO_SUBSTITUTION(p) : (void)(0);
-#else
       free BOOST_PREVENT_MACRO_SUBSTITUTION(pool<UserAllocator>::malloc BOOST_PREVENT_MACRO_SUBSTITUTION());
-#endif
     }
 
     void * malloc BOOST_PREVENT_MACRO_SUBSTITUTION()

Added: sandbox/pool/libs/pool/test/test_bug_6561.cpp
==============================================================================
--- (empty file)
+++ sandbox/pool/libs/pool/test/test_bug_6561.cpp 2012-05-03 14:53:09 EDT (Thu, 03 May 2012)
@@ -0,0 +1,39 @@
+/* Copyright (C) 2012 Étienne Dupuis
+*
+* Use, modification and distribution is subject to the
+* Boost Software License, Version 1.0. (See accompanying
+* file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
+*/
+
+// Test of bug #6561 (https://svn.boost.org/trac/boost/ticket/6561)
+
+#include <boost/pool/pool.hpp>
+#include <boost/pool/static_pool.hpp>
+#include <boost/pool/object_pool.hpp>
+#include <boost/pool/singleton_pool.hpp>
+
+struct dummy
+{
+ int x; ~dummy() { x = 0; }
+};
+
+int main()
+{
+ boost::pool<> p(1, 1);
+ p.free(NULL);
+ p.free(NULL, 4);
+ p.ordered_free(NULL);
+ p.ordered_free(NULL, 4);
+
+ boost::static_pool<> sp(1, 1);
+ sp.free(NULL);
+ sp.free(NULL, 4);
+ sp.ordered_free(NULL);
+ sp.ordered_free(NULL, 4);
+
+ boost::object_pool<dummy> op(1);
+ op.destroy(NULL);
+ op.free(NULL);
+
+ return 0;
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk