Preserve active reservation bounds across smaller reserve() calls and clear stale reservations on rehash(0)

ReserveTableToFitNewSize previously overwrote reservation_size_ with new_size and recomputed reserved_growth_ unconditionally as `new_size - size()`. Calling reserve(M) after reserve(N) (with `size() <= M < N`) shrank both fields below the active N-element guarantee, causing sanitizer bug-detection rehashing to reallocate the backing array before N elements were inserted and causing `erase(begin(), end())` to restore reserved_growth_ to M instead of N. Calling reserve(M) with `M < size()` also underflowed unsigned `M - size()`, setting reserved_growth_ to kReservedGrowthJustRanOut when `M == size() - 1` (forcing an immediate rehash on the next insert) or wrapping it to a large positive count when `M <= size() - 2` (disabling iterator-invalidation generation increments on subsequent unreserved inserts).

In addition, Rehash(0) did not clear reserved_growth_ or reservation_size_ when shrinking or rehashing a previously reserved table, leaving stale reservation counts behind after capacity shrunk.

Update ReserveTableToFitNewSize to advance reservation_size_ monotonically and keep `max(current_reserved_growth, new_size - size())` without unsigned underflow, and clear reserved_growth_ and reservation_size_ in Rehash when `n == 0`.

PiperOrigin-RevId: 1024871812
diff --git a/absl/container/internal/raw_hash_set.cc b/absl/container/internal/raw_hash_set.cc
index 0a8e80f..dd9c8c8 100644
--- a/absl/container/internal/raw_hash_set.cc
+++ b/absl/container/internal/raw_hash_set.cc
@@ -2418,6 +2418,7 @@
   const size_t soo_capacity = policy.alloc_policy->soo_capacity();
 
   if (n == 0) {
+    common.clear_reservation();
     if (cap <= soo_capacity) return;
     if (common.empty()) {
       clear_backing_array();
@@ -2555,8 +2556,7 @@
   const AllocPolicy& alloc_policy = *policy.alloc_policy;
   new_size = std::min(new_size,
                       MaxValidSize(alloc_policy.key_size, policy.slot_size));
-  common.reset_reserved_growth(new_size);
-  common.set_reservation_size(new_size);
+  common.update_for_reserve(new_size);
   ABSL_SWISSTABLE_ASSERT(new_size > policy.alloc_policy->soo_capacity());
   const size_t cap = common.capacity();
   if (ABSL_PREDICT_TRUE(common.empty() &&
diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h
index 8f40637..368fbc3 100644
--- a/absl/container/internal/raw_hash_set.h
+++ b/absl/container/internal/raw_hash_set.h
@@ -1036,6 +1036,18 @@
   void reset_reserved_growth(size_t reservation, size_t size) {
     reserved_growth_ = reservation - size;
   }
+  void update_for_reserve(size_t reservation, size_t size) {
+    const size_t current =
+        reserved_growth_ == kReservedGrowthJustRanOut ? 0 : reserved_growth_;
+    if (reservation > size) {
+      reserved_growth_ = (std::max)(current, reservation - size);
+    }
+    reservation_size_ = (std::max)(reservation_size_, reservation);
+  }
+  void clear_reservation() {
+    reserved_growth_ = 0;
+    reservation_size_ = 0;
+  }
   size_t reserved_growth() const { return reserved_growth_; }
   void set_reserved_growth(size_t r) { reserved_growth_ = r; }
   size_t reservation_size() const { return reservation_size_; }
@@ -1079,6 +1091,8 @@
   void maybe_increment_generation_on_insert() {}
   void increment_generation() {}
   void reset_reserved_growth(size_t, size_t) {}
+  void update_for_reserve(size_t, size_t) {}
+  void clear_reservation() {}
   size_t reserved_growth() const { return 0; }
   void set_reserved_growth(size_t) {}
   size_t reservation_size() const { return 0; }
@@ -1507,6 +1521,9 @@
   void reset_reserved_growth(size_t reservation) {
     CommonFieldsGenerationInfo::reset_reserved_growth(reservation, size());
   }
+  void update_for_reserve(size_t reservation) {
+    CommonFieldsGenerationInfo::update_for_reserve(reservation, size());
+  }
 
   // Returns the number of blocked elements in the table.
   // Blocked elements are located at the end of the table and do not have
diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc
index 970420e..3bbeff0 100644
--- a/absl/container/internal/raw_hash_set_test.cc
+++ b/absl/container/internal/raw_hash_set_test.cc
@@ -6048,6 +6048,74 @@
   test(absl::flat_hash_set<int32_t, ZeroHash>());
 }
 
+TEST(Table, SmallerReservePreservesPriorReservationAndDoesNotUnderflow) {
+  {
+    // A smaller reserve(M) after reserve(N) (with size <= M < N) must not
+    // shrink reserved_growth_ or reservation_size_ below the active N-element
+    // guarantee.
+    IntTable t;
+    t.reserve(20);
+    t.insert(0);
+    t.insert(1);
+    const int64_t* p0 = &*t.find(0);
+    t.reserve(5);
+    for (int i = 2; i < 20; ++i) {
+      t.insert(i);
+    }
+    EXPECT_EQ(&*t.find(0), p0);
+
+    // erase(begin(), end()) must restore reserved_growth_ to 20, not 5.
+    t.erase(t.begin(), t.end());
+    t.insert(0);
+    p0 = &*t.find(0);
+    for (int i = 1; i < 20; ++i) {
+      t.insert(i);
+    }
+    EXPECT_EQ(&*t.find(0), p0);
+  }
+  {
+    // reserve(size - 1) must not underflow reserved_growth_ to
+    // kReservedGrowthJustRanOut and force a spurious rehash on the next insert.
+    IntTable t;
+    t.reserve(20);
+    for (int i = 0; i < 10; ++i) {
+      t.insert(i);
+    }
+    const int64_t* p0 = &*t.find(0);
+    t.reserve(9);
+    t.insert(10);
+    EXPECT_EQ(&*t.find(0), p0);
+  }
+  if (SwisstableGenerationsEnabled()) {
+    // reserve(size - 2) on an unreserved table must not wrap reserved_growth_
+    // to ~size_t{0} - 1 and suppress iterator-invalidation generation checks.
+    IntTable t;
+    for (int i = 0; i < 4; ++i) {
+      t.insert(i);
+    }
+    t.reserve(2);
+    auto it = t.begin();
+    t.insert(4);
+    EXPECT_DEATH_IF_SUPPORTED((void)*it, InvalidIteratorMatcher());
+  }
+}
+
+TEST(Table, RehashZeroClearsStaleReservation) {
+  if (!SwisstableGenerationsEnabled()) {
+    GTEST_SKIP() << "Generations disabled.";
+  }
+  IntTable t;
+  t.reserve(50);
+  t.insert(1);
+  t.insert(2);
+  t.rehash(0);
+  // After shrinking via rehash(0), subsequent insertions are no longer covered
+  // by the old reserve(50) guarantee and must advance the generation counter.
+  auto it = t.begin();
+  t.insert(3);
+  EXPECT_DEATH_IF_SUPPORTED((void)*it, InvalidIteratorMatcher());
+}
+
 }  // namespace
 }  // namespace container_internal
 ABSL_NAMESPACE_END