aboutsummaryrefslogtreecommitdiff
path: root/gdb/completer.c
diff options
context:
space:
mode:
authorAndrew Burgess <andrew.burgess@embecosm.com>2020-04-04 14:54:15 +0100
committerAndrew Burgess <andrew.burgess@embecosm.com>2020-04-15 16:36:28 +0100
commit99f1bc6aaa2810fa4600b1cfd13d2d52678e1a66 (patch)
tree1574f14e84090934908b825d6a4a6ad3254e2675 /gdb/completer.c
parenta0e9b53238c3033222c53b1654da535c0743ab6e (diff)
downloadgdb-99f1bc6aaa2810fa4600b1cfd13d2d52678e1a66.zip
gdb-99f1bc6aaa2810fa4600b1cfd13d2d52678e1a66.tar.gz
gdb-99f1bc6aaa2810fa4600b1cfd13d2d52678e1a66.tar.bz2
gdb: Don't corrupt completions hash when expanding the hash table
Commit: commit 724fd9ba432a20ef2e3f2c0d6060bff131226816 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.
Diffstat (limited to 'gdb/completer.c')
-rw-r--r--gdb/completer.c18
1 files changed, 17 insertions, 1 deletions
diff --git a/gdb/completer.c b/gdb/completer.c
index 67dce30..0dd91a7 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -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);
}