boost/boost-1.77-pool-fix-interger-overflows-in-pool-ordered_malloc.patch

153 lines
6.0 KiB
Diff
Raw Normal View History

2021-09-17 00:03:53 +08:00
From b59c1be697a001a71f6b92660e41d8915eea941d Mon Sep 17 00:00:00 2001
From: Orgad Shaneh <orgads@gmail.com>
Date: Thu, 9 Sep 2021 10:28:13 +0300
Subject: [PATCH] fix integer overflows in pool::ordered_malloc
Fixes trac #6701 (https://svn.boost.org/trac10/ticket/6701).
Originally-by: Jonathan Wakely <jwakely.boost@kayari.org>
---
boost/pool/pool.hpp | 32 +++++++++++++++++++++++---------
libs/pool/test/test_bug_6701.cpp | 27 +++++++++++++++++++++++++++
2 files changed, 50 insertions(+), 9 deletions(-)
create mode 100644 libs/pool/test/test_bug_6701.cpp
diff --git a/boost/pool/pool.hpp b/boost/pool/pool.hpp
index c47b11faf..a899ca0a2 100644
--- a/boost/pool/pool.hpp
+++ b/boost/pool/pool.hpp
@@ -26,6 +26,8 @@
2019-09-30 10:32:42 -04:00
#include <boost/pool/poolfwd.hpp>
+// std::numeric_limits
+#include <boost/limits.hpp>
// boost::integer::static_lcm
#include <boost/integer/common_factor_ct.hpp>
2021-09-17 00:03:53 +08:00
// boost::simple_segregated_storage
@@ -355,6 +357,13 @@ class pool: protected simple_segregated_storage < typename UserAllocator::size_t
return s;
2019-09-30 10:32:42 -04:00
}
+ 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<sizeof(size_type), sizeof(void *)>::value + sizeof(size_type);
+ return (std::numeric_limits<size_type>::max() - POD_size) / alloc_size();
+ }
+
static void * & nextof(void * const ptr)
{ //! \returns Pointer dereferenced.
2021-09-17 00:03:53 +08:00
//! (Provided and used for the sake of code readability :)
@@ -375,6 +384,8 @@ class pool: protected simple_segregated_storage < typename UserAllocator::size_t
2019-09-30 10:32:42 -04:00
//! the first time that object needs to allocate system memory.
//! The default is 32. This parameter may not be 0.
2021-09-17 00:03:53 +08:00
//! \param nmax_size is the maximum number of chunks to allocate in one block.
2019-09-30 10:32:42 -04:00
+ set_next_size(nnext_size);
+ set_max_size(nmax_size);
}
2021-09-17 00:03:53 +08:00
~pool()
@@ -398,8 +409,8 @@ class pool: protected simple_segregated_storage < typename UserAllocator::size_t
2019-09-30 10:32:42 -04:00
}
void set_next_size(const size_type nnext_size)
2021-09-17 00:03:53 +08:00
{ //! 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.
2019-09-30 10:32:42 -04:00
- //! \returns nnext_size.
- next_size = start_size = nnext_size;
+ BOOST_USING_STD_MIN();
+ next_size = start_size = min BOOST_PREVENT_MACRO_SUBSTITUTION(nnext_size, max_chunks());
}
size_type get_max_size() const
2021-09-17 00:03:53 +08:00
{ //! \returns max_size.
@@ -407,7 +418,8 @@ class pool: protected simple_segregated_storage < typename UserAllocator::size_t
}
2019-09-30 10:32:42 -04:00
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
2021-09-17 00:03:53 +08:00
{ //! \returns the requested size passed into the constructor.
@@ -708,9 +720,9 @@ void * pool<UserAllocator>::malloc_need_resize()
2019-09-30 10:32:42 -04:00
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,
2021-09-17 00:03:53 +08:00
store().add_block(node.begin(), node.element_size(), partition_size);
@@ -748,9 +760,9 @@ void * pool<UserAllocator>::ordered_malloc_need_resize()
2019-09-30 10:32:42 -04:00
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,
2021-09-17 00:03:53 +08:00
// (we can use "add_block" here because we know that
@@ -792,6 +804,8 @@ void * pool<UserAllocator>::ordered_malloc(const size_type n)
{ //! Gets address of a chunk n, allocating new memory if not already available.
2019-09-30 10:32:42 -04:00
//! \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();
2021-09-17 00:03:53 +08:00
const size_type total_req_size = n * requested_size;
@@ -840,9 +854,9 @@ void * pool<UserAllocator>::ordered_malloc(const size_type n)
2019-09-30 10:32:42 -04:00
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,
2021-09-17 00:03:53 +08:00
// handle border case.
diff --git a/libs/pool/test/test_bug_6701.cpp b/libs/pool/test/test_bug_6701.cpp
new file mode 100644
index 000000000..e484d3c7e
--- /dev/null
+++ b/libs/pool/test/test_bug_6701.cpp
2019-09-30 10:32:42 -04:00
@@ -0,0 +1,27 @@
+/* Copyright (C) 2012 Étienne Dupuis
2021-09-17 00:03:53 +08:00
+*
+* Use, modification and distribution is subject to the
2019-09-30 10:32:42 -04:00
+* 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 <boost/pool/object_pool.hpp>
+#include <boost/limits.hpp>
+
+int main()
+{
+ boost::pool<> p(1024, std::numeric_limits<size_t>::max() / 768);
+
+ void *x = p.malloc();
+ BOOST_ASSERT(!x);
2021-09-17 00:03:53 +08:00
+
2019-09-30 10:32:42 -04:00
+ BOOST_ASSERT(std::numeric_limits<size_t>::max() / 1024 >= p.get_next_size());
+ BOOST_ASSERT(std::numeric_limits<size_t>::max() / 1024 >= p.get_max_size());
+
+ void *y = p.ordered_malloc(std::numeric_limits<size_t>::max() / 768);
+ BOOST_ASSERT(!y);
+
+ return 0;
+}
2021-09-17 00:03:53 +08:00
--
2.31.1