gdb: Don't corrupt completions hash when expanding the hash table

Commit:

  commit 724fd9ba43
  Date:   Mon Jan 27 17:37:20 2020 +0000

      gdb: Restructure the completion_tracker class

caused the completion hash table to become corrupted if the table ever
needed to grow beyond its original size of 200 elements.

The hash table stores completion_tracker::completion_hash_entry
objects, but hashes them based on their name, which is only one field
of the object.

When possibly inserting a new element we compute the hash with
htab_hash_string of the new elements name, and then lookup matching
elements using htab_find_slot_with_hash.  If there's not matching
element we create a completion_hash_entry object within the hash
table.

However, when we allocate the hash we pass htab_hash_string to
htab_create_alloc as the hash function, and this is not OK.  This
means that when the hash table needs to grow, existing elements within
the hash are re-hashed by passing the completion_hash_entry pointer to
htab_hash_string, which obviously does not do what we expect.

The solution is to create a new hash function that takes a pointer to
a completion_hash_entry, and then calls htab_hash_string on the name
of the entry only.

This regression was spotted when running the gdb.base/completion.exp
test on the aarch64 target.

gdb/ChangeLog:

	* completer.c (class completion_tracker::completion_hash_entry)
	<hash_name>: New member function.
	(completion_tracker::discard_completions): New callback to hash a
	completion_hash_entry, pass this to htab_create_alloc.

gdb/testsuite/ChangeLog:

	* gdb.base/many-completions.exp: New file.
This commit is contained in:
Andrew Burgess
2020-04-04 14:54:15 +01:00
parent a0e9b53238
commit 99f1bc6aaa
4 changed files with 120 additions and 1 deletions

View File

@@ -82,6 +82,12 @@ public:
return strcmp (m_name.get (), str) == 0;
}
/* Return the hash value based on the name of the entry. */
hashval_t hash_name () const
{
return htab_hash_string (m_name.get ());
}
/* A static function that can be passed to the htab hash system to be
used as a callback that deletes an item from the hash. */
static void deleter (void *arg)
@@ -1602,8 +1608,18 @@ completion_tracker::discard_completions ()
return entry->is_name_eq (name_str);
};
/* Callback used by the hash table to compute the hash value for an
existing entry. This is needed when expanding the hash table. */
static auto entry_hash_func
= [] (const void *arg) -> hashval_t
{
const completion_hash_entry *entry
= (const completion_hash_entry *) arg;
return entry->hash_name ();
};
m_entries_hash = htab_create_alloc (INITIAL_COMPLETION_HTAB_SIZE,
htab_hash_string, entry_eq_func,
entry_hash_func, entry_eq_func,
completion_hash_entry::deleter,
xcalloc, xfree);
}