diff --git a/doc/circular_buffer.qbk b/doc/circular_buffer.qbk index 65f0561a..4a882c15 100644 --- a/doc/circular_buffer.qbk +++ b/doc/circular_buffer.qbk @@ -473,6 +473,14 @@ Pull requests are welcome at [@https://github.com/boostorg/circular_buffer boost [section:release Release Notes] +[h4 Boost 1.94] + +* `insert`, `rinsert`, `resize` and `rresize`, and `push_back` and `push_front` of + `circular_buffer_space_optimized`, now insert the value that `item` had at the time of the call when + `item` refers to an element of the same container, as `std::vector` does. Previously the elements were + moved, or the storage reallocated, before `item` was read, so a neighbouring value was inserted or freed + memory was read. + [h4 Boost 1.91] * Avoided a GCC 4.x warning about calling `memset` with a constant size of zero. diff --git a/include/boost/circular_buffer/base.hpp b/include/boost/circular_buffer/base.hpp index 47441544..8b44d15b 100644 --- a/include/boost/circular_buffer/base.hpp +++ b/include/boost/circular_buffer/base.hpp @@ -22,7 +22,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -33,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -918,8 +921,14 @@ private empty_value */ void resize(size_type new_size, param_value_type item = value_type()) { if (new_size > size()) { - if (new_size > capacity()) + if (new_size > capacity()) { + if (is_stored(item)) { + value_type copy(item); // set_capacity() frees the storage that item is in + resize(new_size, copy); + return; + } set_capacity(new_size); + } insert(end(), new_size - size(), item); } else { iterator e = end(); @@ -993,8 +1002,14 @@ private empty_value */ void rresize(size_type new_size, param_value_type item = value_type()) { if (new_size > size()) { - if (new_size > capacity()) + if (new_size > capacity()) { + if (is_stored(item)) { + value_type copy(item); // set_capacity() frees the storage that item is in + rresize(new_size, copy); + return; + } set_capacity(new_size); + } rinsert(begin(), new_size - size(), item); } else { rerase(begin(), end() - new_size); @@ -1670,6 +1685,10 @@ private empty_value rinsert(iterator, InputIterator, InputIterator) */ iterator insert(iterator pos, param_value_type item) { + if (is_stored(item)) { + value_type copy(item); // the elements are moved before item is read + return insert(pos, copy); + } return insert_impl(pos, item); } @@ -1789,6 +1808,11 @@ private empty_value return; if (n > copy) n = copy; + if (is_stored(item)) { + value_type item_copy(item); // the elements are moved before item is read + insert_n(pos, n, cb_details::item_wrapper(item_copy)); + return; + } insert_n(pos, n, cb_details::item_wrapper(item)); } @@ -1925,6 +1949,10 @@ private empty_value insert(iterator, InputIterator, InputIterator) */ iterator rinsert(iterator pos, param_value_type item) { + if (is_stored(item)) { + value_type copy(item); // the elements are moved before item is read + return rinsert(pos, copy); + } return rinsert_impl(pos, item); } @@ -2034,6 +2062,11 @@ private empty_value */ void rinsert(iterator pos, size_type n, param_value_type item) { BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator + if (n != 0 && is_stored(item)) { + value_type copy(item); // the elements are moved before item is read + rinsert_n(pos, n, cb_details::item_wrapper(copy)); + return; + } rinsert_n(pos, n, cb_details::item_wrapper(item)); } @@ -2335,6 +2368,16 @@ private empty_value m_size = 0; } +protected: + /*! INTERNAL ONLY */ + bool is_stored(param_value_type item) const BOOST_NOEXCEPT { + if (m_buff == 0) + return false; + std::less less; + const value_type* p = boost::addressof(item); + return !less(p, boost::to_address(m_buff)) && less(p, boost::to_address(m_end)); + } + private: // Helper methods diff --git a/include/boost/circular_buffer/space_optimized.hpp b/include/boost/circular_buffer/space_optimized.hpp index b413e932..46bcd3b2 100644 --- a/include/boost/circular_buffer/space_optimized.hpp +++ b/include/boost/circular_buffer/space_optimized.hpp @@ -782,6 +782,11 @@ public:
pop_front() */ void push_back(param_value_type item) { + if (circular_buffer::is_stored(item)) { + value_type copy(item); // check_low_capacity() may free the storage that item is in + push_back(copy); + return; + } check_low_capacity(); circular_buffer::push_back(item); } @@ -856,6 +861,11 @@ public:
pop_front() */ void push_front(param_value_type item) { + if (circular_buffer::is_stored(item)) { + value_type copy(item); // check_low_capacity() may free the storage that item is in + push_front(copy); + return; + } check_low_capacity(); circular_buffer::push_front(item); } @@ -988,6 +998,10 @@ public:
rinsert(iterator, InputIterator, InputIterator) */ iterator insert(iterator pos, param_value_type item) { + if (circular_buffer::is_stored(item)) { + value_type copy(item); // check_low_capacity() may free the storage that item is in + return insert(pos, copy); + } size_type index = pos - begin(); check_low_capacity(); return circular_buffer::insert(begin() + index, item); @@ -1110,6 +1124,11 @@ public:
rinsert(iterator, InputIterator, InputIterator) */ void insert(iterator pos, size_type n, param_value_type item) { + if (circular_buffer::is_stored(item)) { + value_type copy(item); // check_low_capacity() may free the storage that item is in + insert(pos, n, copy); + return; + } size_type index = pos - begin(); check_low_capacity(n); circular_buffer::insert(begin() + index, n, item); @@ -1199,6 +1218,10 @@ public:
insert(iterator, InputIterator, InputIterator) */ iterator rinsert(iterator pos, param_value_type item) { + if (circular_buffer::is_stored(item)) { + value_type copy(item); // check_low_capacity() may free the storage that item is in + return rinsert(pos, copy); + } size_type index = pos - begin(); check_low_capacity(); return circular_buffer::rinsert(begin() + index, item); @@ -1321,6 +1344,11 @@ public:
insert(iterator, InputIterator, InputIterator) */ void rinsert(iterator pos, size_type n, param_value_type item) { + if (circular_buffer::is_stored(item)) { + value_type copy(item); // check_low_capacity() may free the storage that item is in + rinsert(pos, n, copy); + return; + } size_type index = pos - begin(); check_low_capacity(n); circular_buffer::rinsert(begin() + index, n, item); diff --git a/test/common.ipp b/test/common.ipp index e26babe5..6e6f22e8 100644 --- a/test/common.ipp +++ b/test/common.ipp @@ -2442,6 +2442,69 @@ void check_containers_exception_specifications() { } // add common tests into a test suite +// Fill with 1 2 3 4, with the first element away from the start of the storage. +template +CB& alias_fill(CB& cb) { + for (int i = 0; i < 3; ++i) + cb.push_back(0); + for (int i = 1; i <= 4; ++i) + cb.push_back(i); + while (cb.size() > 4) + cb.pop_front(); + return cb; +} + +#define CB_TEST_ALIAS(cap, expr, expected) \ + do { \ + CB_CONTAINER cb(cap); \ + alias_fill(cb).expr; \ + BOOST_TEST_ALL_EQ(cb.begin(), cb.end(), expected, expected + sizeof(expected) / sizeof(expected[0])); \ + } while (false) + +void insert_element_of_itself_test() { + + // Like std::vector, inserting an element of the container inserts the value it had before the call. + // (The unsigned counts select insert(pos, n, item), not the integral form of insert(pos, first, last).) + const int e1[] = {1, 3, 2, 3, 4}; + CB_TEST_ALIAS(8, insert(cb.begin() + 1, cb[2]), e1); + const int e2[] = {1, 4, 2, 3, 4}; + CB_TEST_ALIAS(8, insert(cb.begin() + 1, 1u, cb[3]), e2); + const int e3[] = {1, 3, 3, 2, 3, 4}; + CB_TEST_ALIAS(8, insert(cb.begin() + 1, 2u, cb[2]), e3); + const int e4[] = {1, 2, 1, 3, 4}; + CB_TEST_ALIAS(8, rinsert(cb.begin() + 2, cb[0]), e4); + const int e5[] = {1, 2, 3, 1, 4}; + CB_TEST_ALIAS(8, rinsert(cb.begin() + 3, 1u, cb[0]), e5); + const int e6[] = {1, 2, 2, 2, 3, 4}; + CB_TEST_ALIAS(8, rinsert(cb.begin() + 2, 2u, cb[1]), e6); + const int e7[] = {1, 2, 3, 4, 1}; + CB_TEST_ALIAS(8, push_back(cb[0]), e7); + const int e8[] = {4, 1, 2, 3, 4}; + CB_TEST_ALIAS(8, push_front(cb[3]), e8); + + // Full: the element that the item refers to may be the one that is overwritten. + const int e9[] = {2, 1, 3, 4}; + CB_TEST_ALIAS(4, insert(cb.begin() + 2, cb[0]), e9); + const int e10[] = {2, 1, 3, 4}; + CB_TEST_ALIAS(4, insert(cb.begin() + 2, 1u, cb[0]), e10); + const int e11[] = {1, 2, 4, 3}; + CB_TEST_ALIAS(4, rinsert(cb.begin() + 2, cb[3]), e11); + const int e12[] = {1, 2, 4, 3}; + CB_TEST_ALIAS(4, rinsert(cb.begin() + 2, 1u, cb[3]), e12); + const int e13[] = {2, 3, 4, 1}; + CB_TEST_ALIAS(4, push_back(cb[0]), e13); + const int e14[] = {4, 1, 2, 3}; + CB_TEST_ALIAS(4, push_front(cb[3]), e14); + + // Growing beyond the capacity reallocates before the item is read. + const int e15[] = {1, 2, 3, 4, 1, 1}; + CB_TEST_ALIAS(4, resize(6, cb[0]), e15); + const int e16[] = {4, 4, 1, 2, 3, 4}; + CB_TEST_ALIAS(4, rresize(6, cb[3]), e16); +} + +#undef CB_TEST_ALIAS + void run_common_tests() { basic_test(); @@ -2475,6 +2538,7 @@ void run_common_tests() rinsert_test(); rinsert_n_test(); rinsert_range_test(); + insert_element_of_itself_test(); erase_test(); erase_range_test(); rerase_test(); diff --git a/test/test.hpp b/test/test.hpp index dfdd5de0..f0867b87 100644 --- a/test/test.hpp +++ b/test/test.hpp @@ -23,6 +23,28 @@ #if !defined(BOOST_NO_EXCEPTIONS) #include #endif +#if defined(_MSC_VER) && defined(_DEBUG) + #include + #include +#endif + +#if defined(_MSC_VER) && defined(_DEBUG) +namespace { + +// Report CRT asserts and errors on stderr instead of in a dialog, which hangs an unattended +// test until it times out and hides the message. +struct cb_no_crt_dialogs { + cb_no_crt_dialogs() { + _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE); + _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); + _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE); + _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR); + _set_abort_behavior(0, _CALL_REPORTFAULT); + } +} cb_no_crt_dialogs_instance; + +} // namespace +#endif // Integer (substitute for int) - more appropriate for testing class MyInteger {