diff options
-rw-r--r-- | base/pickle.cc | 3 | ||||
-rw-r--r-- | base/pickle_unittest.cc | 7 |
2 files changed, 6 insertions, 4 deletions
diff --git a/base/pickle.cc b/base/pickle.cc index d525e3f..1aeb1f4 100644 --- a/base/pickle.cc +++ b/base/pickle.cc @@ -263,7 +263,8 @@ char* Pickle::BeginWrite(size_t length) { size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); size_t new_size = offset + length; - if (header_size_ + new_size > capacity_ && !Resize(header_size_ + new_size)) + size_t needed_size = header_size_ + new_size; + if (needed_size > capacity_ && !Resize(std::max(capacity_ * 2, needed_size))) return NULL; #ifdef ARCH_CPU_64_BITS diff --git a/base/pickle_unittest.cc b/base/pickle_unittest.cc index a3fa1eb..48ab1cd 100644 --- a/base/pickle_unittest.cc +++ b/base/pickle_unittest.cc @@ -167,7 +167,8 @@ TEST(PickleTest, Resize) { static_cast<int>(payload_size_after_header - sizeof(uint32))); size_t cur_payload = payload_size_after_header; - EXPECT_EQ(pickle.capacity(), unit); + // note: we assume 'unit' is a power of 2 + EXPECT_EQ(unit, pickle.capacity()); EXPECT_EQ(pickle.payload_size(), payload_size_after_header); // fill out a full page (noting data header) @@ -176,10 +177,10 @@ TEST(PickleTest, Resize) { EXPECT_EQ(unit * 2, pickle.capacity()); EXPECT_EQ(cur_payload, pickle.payload_size()); - // one more byte should expand the capacity by one unit + // one more byte should double the capacity pickle.WriteData(data_ptr, 1); cur_payload += 5; - EXPECT_EQ(unit * 3, pickle.capacity()); + EXPECT_EQ(unit * 4, pickle.capacity()); EXPECT_EQ(cur_payload, pickle.payload_size()); } |