https://src.fedoraproject.org/rpms/boost/raw/master/f/boost-1.58.0-pool.patch https://bugzilla.redhat.com/show_bug.cgi?id=828856 https://bugs.gentoo.org/620468 https://svn.boost.org/trac10/ticket/6701 --- a/boost/pool/pool.hpp +++ b/boost/pool/pool.hpp @@ -27,4 +27,6 @@ #include +// std::numeric_limits +#include // boost::integer::static_lcm #include @@ -356,4 +358,11 @@ } + size_type max_chunks() const + { //! Calculated maximum number of memory chunks that can be allocated in a single call by this Pool. + size_type partition_size = alloc_size(); + size_type POD_size = integer::static_lcm::value + sizeof(size_type); + return (std::numeric_limits::max() - POD_size) / alloc_size(); + } + static void * & nextof(void * const ptr) { //! \returns Pointer dereferenced. @@ -375,5 +384,7 @@ //! the first time that object needs to allocate system memory. //! The default is 32. This parameter may not be 0. - //! \param nmax_size is the maximum number of chunks to allocate in one block. + //! \param nmax_size is the maximum number of chunks to allocate in one block. + set_next_size(nnext_size); + set_max_size(nmax_size); } @@ -398,7 +409,7 @@ } void set_next_size(const size_type nnext_size) - { //! Set number of chunks to request from the system the next time that object needs to allocate system memory. This value should never be set to 0. - //! \returns nnext_size. - next_size = start_size = nnext_size; + { //! Set number of chunks to request from the system the next time that object needs to allocate system memory. This value should never be set to 0. + BOOST_USING_STD_MIN(); + next_size = start_size = min BOOST_PREVENT_MACRO_SUBSTITUTION(nnext_size, max_chunks()); } size_type get_max_size() const @@ -408,5 +419,6 @@ void set_max_size(const size_type nmax_size) { //! Set max_size. - max_size = nmax_size; + BOOST_USING_STD_MIN(); + max_size = min BOOST_PREVENT_MACRO_SUBSTITUTION(nmax_size, max_chunks()); } size_type get_requested_size() const @@ -709,7 +721,7 @@ BOOST_USING_STD_MIN(); if(!max_size) - next_size <<= 1; + set_next_size(next_size << 1); else if( next_size*partition_size/requested_size < max_size) - next_size = min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size*requested_size/ partition_size); + set_next_size(min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size * requested_size / partition_size)); // initialize it, @@ -749,7 +761,7 @@ BOOST_USING_STD_MIN(); if(!max_size) - next_size <<= 1; + set_next_size(next_size << 1); else if( next_size*partition_size/requested_size < max_size) - next_size = min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size*requested_size/ partition_size); + set_next_size(min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size * requested_size / partition_size)); // initialize it, @@ -793,4 +805,6 @@ //! \returns Address of chunk n if allocated ok. //! \returns 0 if not enough memory for n chunks. + if (n > max_chunks()) + return 0; const size_type partition_size = alloc_size(); @@ -841,7 +855,7 @@ BOOST_USING_STD_MIN(); if(!max_size) - next_size <<= 1; + set_next_size(next_size << 1); else if( next_size*partition_size/requested_size < max_size) - next_size = min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size*requested_size/ partition_size); + set_next_size(min BOOST_PREVENT_MACRO_SUBSTITUTION(next_size << 1, max_size * requested_size / partition_size)); // insert it into the list, --- a/libs/pool/test/test_bug_6701.cpp +++ b/libs/pool/test/test_bug_6701.cpp @@ -0,0 +1,27 @@ +/* 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 #6701 (https://svn.boost.org/trac/boost/ticket/6701) + +#include +#include + +int main() +{ + boost::pool<> p(1024, std::numeric_limits::max() / 768); + + void *x = p.malloc(); + BOOST_ASSERT(!x); + + BOOST_ASSERT(std::numeric_limits::max() / 1024 >= p.get_next_size()); + BOOST_ASSERT(std::numeric_limits::max() / 1024 >= p.get_max_size()); + + void *y = p.ordered_malloc(std::numeric_limits::max() / 768); + BOOST_ASSERT(!y); + + return 0; +}