Merge "tp: add is_kthread as a process.arg when parsing procfs packets" into main
diff --git a/src/trace_processor/containers/bit_vector.cc b/src/trace_processor/containers/bit_vector.cc
index 28fe966..f2e502f 100644
--- a/src/trace_processor/containers/bit_vector.cc
+++ b/src/trace_processor/containers/bit_vector.cc
@@ -170,21 +170,28 @@
     if (filler) {
       // If the new space should be filled with ones, then set all the bits
       // between the address of the old size and the new last address.
-      const Address& start = IndexToAddress(old_size);
-      Set(start, last_addr);
+      const Address& start_filler = IndexToAddress(old_size);
+      Set(start_filler, last_addr);
+
+      // If `start_filler` is at the start of a block, we need to update the
+      // last old block `counts_` manually.
+      if (old_size % kBitsInBlock == 0) {
+        counts_[start_filler.block_idx] = CountSetBits();
+      }
 
       // We then need to update the counts vector to match the changes we
       // made to the blocks.
 
       // We start by adding the bits we set in the first block to the
       // cummulative count before the range we changed.
-      Address end_of_block = {start.block_idx,
+      Address end_of_block = {start_filler.block_idx,
                               {Block::kWords - 1, BitWord::kBits - 1}};
       uint32_t count_in_block_after_end =
-          AddressToIndex(end_of_block) - AddressToIndex(start) + 1;
+          AddressToIndex(end_of_block) - AddressToIndex(start_filler) + 1;
       uint32_t set_count = CountSetBits() + count_in_block_after_end;
 
-      for (uint32_t i = start.block_idx + 1; i <= last_addr.block_idx; ++i) {
+      for (uint32_t i = start_filler.block_idx + 1; i <= last_addr.block_idx;
+           ++i) {
         // Set the count to the cummulative count so far.
         counts_[i] = set_count;
 
diff --git a/src/trace_processor/containers/bit_vector_unittest.cc b/src/trace_processor/containers/bit_vector_unittest.cc
index f5c6d1c..191beca 100644
--- a/src/trace_processor/containers/bit_vector_unittest.cc
+++ b/src/trace_processor/containers/bit_vector_unittest.cc
@@ -785,6 +785,14 @@
   ASSERT_THAT(bv.GetSetBitIndices(), IsEmpty());
 }
 
+TEST(BitVectorUnittest, ResizeBug) {
+  uint32_t size = 8 * 8 * 8;
+  BitVector bv(size, true);
+  bv.Resize(size + 5, true);
+  ASSERT_EQ(bv.size(), size + 5);
+  ASSERT_EQ(bv.CountSetBits(), size + 5);
+}
+
 TEST(BitVectorUnittest, SerializeSimple) {
   BitVector bv{1, 0, 1, 0, 1, 0, 1};
   protozero::HeapBuffered<protos::pbzero::SerializedColumn::BitVector> msg;