Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix bug in reserve() and shrink_to_fit(). #26

Closed
wants to merge 1 commit into from

3 participants

@celeraone

impl_.e_ += newB - impl_.b_;
fails when the difference between newB and impl_.b_ isn't a multiple
of sizeof(T).

Concrete example: With
sizeof(T) = 32, alignof(T) = 8, newB = 40, impl_.b_ = 0, impl_.e_ = 0
the old code would set impl_.e_ to 32 and impl_.b_ to 40.

@ckamm ckamm Fix bug in reserve() and shrink_to_fit().
impl_.e_ += newB - impl_.b_;
fails when the difference between newB and impl_.b_ isn't a multiple
of sizeof(T).

Concrete example: With
sizeof(T) = 32, alignof(T) = 8, newB = 40, impl_.b_ = 0, impl_.e_ = 0
the old code would set impl_.e_ to 32 and impl_.b_ to 40.
4787e1a
@jdelong
Collaborator

Thanks!

I merged this upstream.

@jdelong jdelong closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 25, 2013
  1. @ckamm

    Fix bug in reserve() and shrink_to_fit().

    ckamm authored
    impl_.e_ += newB - impl_.b_;
    fails when the difference between newB and impl_.b_ isn't a multiple
    of sizeof(T).
    
    Concrete example: With
    sizeof(T) = 32, alignof(T) = 8, newB = 40, impl_.b_ = 0, impl_.e_ = 0
    the old code would set impl_.e_ to 32 and impl_.b_ to 40.
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 2 deletions.
  1. +2 −2 folly/FBVector.h
  2. +12 −0 folly/test/FBVectorTest.cpp
View
4 folly/FBVector.h
@@ -1095,7 +1095,7 @@ class fbvector : private boost::totally_ordered<fbvector<T, Allocator>> {
if (impl_.b_)
M_deallocate(impl_.b_, impl_.z_ - impl_.b_);
impl_.z_ = newB + newCap;
- impl_.e_ += newB - impl_.b_; // speed hax
+ impl_.e_ = newB + (impl_.e_ - impl_.b_);
impl_.b_ = newB;
}
@@ -1128,7 +1128,7 @@ class fbvector : private boost::totally_ordered<fbvector<T, Allocator>> {
if (impl_.b_)
M_deallocate(impl_.b_, impl_.z_ - impl_.b_);
impl_.z_ = newB + newCap;
- impl_.e_ += newB - impl_.b_; // speed hax
+ impl_.e_ = newB + (impl_.e_ - impl_.b_);
impl_.b_ = newB;
}
}
View
12 folly/test/FBVectorTest.cpp
@@ -245,6 +245,18 @@ TEST(FBVector, move_iterator) {
EXPECT_EQ(fbvi3, base);
}
+TEST(FBVector, reserve_consistency) {
+ struct S { int64_t a, b, c, d; };
+
+ fbvector<S> fb1;
+ for (size_t i = 0; i < 1000; ++i)
+ {
+ fb1.reserve(1);
+ EXPECT_EQ(fb1.size(), 0);
+ fb1.shrink_to_fit();
+ }
+}
+
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
google::ParseCommandLineFlags(&argc, &argv, true);
Something went wrong with that request. Please try again.