Skip to content

Commit

Permalink
Fixes #295 ("Bug in small_vector::swap")
Browse files Browse the repository at this point in the history
  • Loading branch information
igaztanaga committed Nov 21, 2024
1 parent 83b8d57 commit a0848ce
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
1 change: 1 addition & 0 deletions doc/container.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,7 @@ use [*Boost.Container]? There are several reasons for that:
* Fixed bugs/issues:
* [@https://github.com/boostorg/container/issues/261 GitHub #261: ['"End iterators are not dereferencable"]].
* [@https://github.com/boostorg/container/issues/288 GitHub #288: ['"Compile error when using flat_map::extract_sequence with small_vector"]].
* [@https://github.com/boostorg/container/issues/295 GitHub #295: ['"Bug in small_vector::swap"]].

[endsect]

Expand Down
4 changes: 2 additions & 2 deletions include/boost/container/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2765,7 +2765,7 @@ class vector
//Move internal memory data to the internal memory data of the target, this can throw
BOOST_ASSERT(extmem.capacity() >= intmem.size());
::boost::container::uninitialized_move_alloc_n
(intmem.get_stored_allocator(), this->priv_raw_begin(), intmem.size(), extmem.priv_raw_begin());
(intmem.get_stored_allocator(), intmem.priv_raw_begin(), intmem.size(), extmem.priv_raw_begin());

//Exception not thrown, commit new state
extmem.m_holder.set_stored_size(intmem.size());
Expand All @@ -2776,7 +2776,7 @@ class vector

//Destroy moved elements from intmem
boost::container::destroy_alloc_n
( intmem.get_stored_allocator(), this->priv_raw_begin()
( intmem.get_stored_allocator(), intmem.priv_raw_begin()
, intmem.size());

//Adopt dynamic buffer
Expand Down
31 changes: 16 additions & 15 deletions test/small_vector_test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2004-2013. Distributed under the Boost
Expand Down Expand Up @@ -297,10 +298,10 @@ bool test_swap()
v.push_back(int(i));
}
vec w;
const std::size_t v_size = v.size();
const std::size_t w_size = w.size();
vec v_copy = v;
vec w_copy = w;
v.swap(w);
if(v.size() != w_size || w.size() != v_size)
if (w != v_copy || v != w_copy)
return false;
}
{ //v smaller than static capacity, w empty
Expand All @@ -309,10 +310,10 @@ bool test_swap()
v.push_back(int(i));
}
vec w;
const std::size_t v_size = v.size();
const std::size_t w_size = w.size();
vec v_copy = v;
vec w_copy = w;
v.swap(w);
if(v.size() != w_size || w.size() != v_size)
if (w != v_copy || v != w_copy)
return false;
}
{ //v bigger than static capacity, w enough capacity for static
Expand All @@ -324,10 +325,10 @@ bool test_swap()
for (std::size_t i = 0, max = w.capacity() / 2; i != max; ++i) {
w.push_back(int(i));
}
const std::size_t v_size = v.size();
const std::size_t w_size = w.size();
vec v_copy = v;
vec w_copy = w;
v.swap(w);
if (v.size() != w_size || w.size() != v_size)
if (w != v_copy || v != w_copy)
return false;
}
{ //v & w smaller than static capacity
Expand All @@ -339,10 +340,10 @@ bool test_swap()
for(std::size_t i = 0, max = w.capacity()/2; i != max; ++i){
w.push_back(int(i));
}
const std::size_t v_size = v.size();
const std::size_t w_size = w.size();
vec v_copy = v;
vec w_copy = w;
v.swap(w);
if(v.size() != w_size || w.size() != v_size)
if (w != v_copy || v != w_copy)
return false;
}
{ //v & w bigger than static capacity
Expand All @@ -354,10 +355,10 @@ bool test_swap()
for(std::size_t i = 0, max = w.capacity()*2; i != max; ++i){
w.push_back(int(i));
}
const std::size_t v_size = v.size();
const std::size_t w_size = w.size();
vec v_copy = v;
vec w_copy = w;
v.swap(w);
if(v.size() != w_size || w.size() != v_size)
if (w != v_copy || v != w_copy)
return false;
}

Expand Down

0 comments on commit a0848ce

Please sign in to comment.