Permalink
Browse files

Merge #7888: prevector: fix 2 bugs in currently unreached code paths

a7af72a prevector::swap: fix (unreached) data corruption (Kaz Wesley)
4ed41a2 test prevector::swap (Kaz Wesley)
1e2c29f prevector: destroy elements only via erase() (Kaz Wesley)
  • Loading branch information...
laanwj committed Apr 18, 2016
2 parents 73fc922 + a7af72a commit ec870e13991fa5f143308485dda0272eef9f0a3f
Showing with 18 additions and 16 deletions.
  1. +5 −14 src/prevector.h
  2. +13 −2 src/test/prevector_tests.cpp
View
@@ -298,9 +298,8 @@ class prevector {
}
void resize(size_type new_size) {
while (size() > new_size) {
item_ptr(size() - 1)->~T();
_size--;
if (size() > new_size) {
erase(item_ptr(new_size), end());
}
if (new_size > capacity()) {
change_capacity(new_size);
@@ -368,10 +367,7 @@ class prevector {
}
iterator erase(iterator pos) {
(*pos).~T();
memmove(&(*pos), &(*pos) + 1, ((char*)&(*end())) - ((char*)(1 + &(*pos))));
_size--;
return pos;
return erase(pos, pos + 1);
}
iterator erase(iterator first, iterator last) {
@@ -396,7 +392,7 @@ class prevector {
}
void pop_back() {
_size--;
erase(end() - 1, end());
}
T& front() {
@@ -416,12 +412,7 @@ class prevector {
}
void swap(prevector<N, T, Size, Diff>& other) {
if (_size & other._size & 1) {
std::swap(_union.capacity, other._union.capacity);
std::swap(_union.indirect, other._union.indirect);
} else {
std::swap(_union, other._union);
}
std::swap(_union, other._union);
std::swap(_size, other._size);
}
@@ -19,9 +19,11 @@ template<unsigned int N, typename T>
class prevector_tester {
typedef std::vector<T> realtype;
realtype real_vector;
realtype real_vector_alt;
typedef prevector<N, T> pretype;
pretype pre_vector;
pretype pre_vector_alt;
typedef typename pretype::size_type Size;
@@ -149,6 +151,12 @@ class prevector_tester {
pre_vector.shrink_to_fit();
test();
}
void swap() {
real_vector.swap(real_vector_alt);
pre_vector.swap(pre_vector_alt);
test();
}
};
BOOST_AUTO_TEST_CASE(PrevectorTestInt)
@@ -204,12 +212,15 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt)
if (test.size() > 0) {
test.update(insecure_rand() % test.size(), insecure_rand());
}
if (((r >> 11) & 1024) == 11) {
if (((r >> 11) % 1024) == 11) {
test.clear();
}
if (((r >> 21) & 512) == 12) {
if (((r >> 21) % 512) == 12) {
test.assign(insecure_rand() % 32, insecure_rand());
}
if (((r >> 15) % 64) == 3) {
test.swap();
}
}
}
}

0 comments on commit ec870e1

Please sign in to comment.