Skip to content

Commit ad07704

Browse files
authored
Fix infinite loop in flatmap resizing when bucket count is a power of two (#3071)
1 parent f949962 commit ad07704

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/butil/containers/flat_map_inl.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,13 @@ template <typename _K, typename _T, typename _H, typename _E,
714714
optional<typename FlatMap<_K, _T, _H, _E, _S, _A, _M>::NewBucketsInfo>
715715
FlatMap<_K, _T, _H, _E, _S, _A, _M>::new_buckets_and_thumbnail(size_t size,
716716
size_t new_nbucket) {
717+
size_t bump = 0;
717718
do {
718-
new_nbucket = flatmap_round(new_nbucket);
719+
// The first iteration uses 'new_nbucket + 0' ensures that when new_nbucket is a power
720+
// of 2 and is already sufficient to accommodate `size`, it does not need to be doubled.
721+
// Subsequent use of 'new_nbucket + 1' avoids an infinite loop.
722+
new_nbucket = flatmap_round(new_nbucket + bump);
723+
bump = 1;
719724
} while (is_too_crowded(size, new_nbucket, _load_factor));
720725
if (_nbucket == new_nbucket) {
721726
return nullopt;

0 commit comments

Comments
 (0)