Skip to content

Commit a76d133

Browse files
jeisingerCommit bot
authored andcommitted
Fix incorrect parameter to HasSufficientCapacity
It takes the number of additional elements, not the total target capacity. Also, avoid right-shifting a negative integer as this is undefined in general BUG=v8:4909 R=verwaest@chromium.org Review-Url: https://codereview.chromium.org/2162333002 Cr-Commit-Position: refs/heads/master@{#37901}
1 parent 99d1e5d commit a76d133

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/objects.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16277,7 +16277,7 @@ Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
1627716277
int capacity = table->Capacity();
1627816278
int nof = table->NumberOfElements() + n;
1627916279

16280-
if (table->HasSufficientCapacity(n)) return table;
16280+
if (table->HasSufficientCapacityToAdd(n)) return table;
1628116281

1628216282
const int kMinCapacityForPretenure = 256;
1628316283
bool should_pretenure = pretenure == TENURED ||
@@ -16293,16 +16293,16 @@ Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
1629316293
return new_table;
1629416294
}
1629516295

16296-
1629716296
template <typename Derived, typename Shape, typename Key>
16298-
bool HashTable<Derived, Shape, Key>::HasSufficientCapacity(int n) {
16297+
bool HashTable<Derived, Shape, Key>::HasSufficientCapacityToAdd(
16298+
int number_of_additional_elements) {
1629916299
int capacity = Capacity();
16300-
int nof = NumberOfElements() + n;
16300+
int nof = NumberOfElements() + number_of_additional_elements;
1630116301
int nod = NumberOfDeletedElements();
1630216302
// Return true if:
16303-
// 50% is still free after adding n elements and
16303+
// 50% is still free after adding number_of_additional_elements elements and
1630416304
// at most 50% of the free elements are deleted elements.
16305-
if (nod <= (capacity - nof) >> 1) {
16305+
if ((nof < capacity) && ((nod <= (capacity - nof) >> 1))) {
1630616306
int needed_free = nof >> 1;
1630716307
if (nof + needed_free <= capacity) return true;
1630816308
}
@@ -17274,7 +17274,7 @@ void Dictionary<Derived, Shape, Key>::SetRequiresCopyOnCapacityChange() {
1727417274
DCHECK_EQ(0, DerivedHashTable::NumberOfDeletedElements());
1727517275
// Make sure that HashTable::EnsureCapacity will create a copy.
1727617276
DerivedHashTable::SetNumberOfDeletedElements(DerivedHashTable::Capacity());
17277-
DCHECK(!DerivedHashTable::HasSufficientCapacity(1));
17277+
DCHECK(!DerivedHashTable::HasSufficientCapacityToAdd(1));
1727817278
}
1727917279

1728017280

@@ -17694,8 +17694,8 @@ Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
1769417694
}
1769517695
// If we're out of luck, we didn't get a GC recently, and so rehashing
1769617696
// isn't enough to avoid a crash.
17697-
int nof = table->NumberOfElements() + 1;
17698-
if (!table->HasSufficientCapacity(nof)) {
17697+
if (!table->HasSufficientCapacityToAdd(1)) {
17698+
int nof = table->NumberOfElements() + 1;
1769917699
int capacity = ObjectHashTable::ComputeCapacity(nof * 2);
1770017700
if (capacity > ObjectHashTable::kMaxCapacity) {
1770117701
for (size_t i = 0; i < 2; ++i) {

src/objects.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3293,7 +3293,7 @@ class HashTable : public HashTableBase {
32933293
PretenureFlag pretenure = NOT_TENURED);
32943294

32953295
// Returns true if this table has sufficient capacity for adding n elements.
3296-
bool HasSufficientCapacity(int n);
3296+
bool HasSufficientCapacityToAdd(int number_of_additional_elements);
32973297

32983298
// Sets the capacity of the hash table.
32993299
void SetCapacity(int capacity) {

0 commit comments

Comments
 (0)