public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [RFA] Fix leak of symbol name in block_symbol_cache
@ 2019-12-01 19:10 Philippe Waroquiers
  2019-12-02 22:24 ` Tom Tromey
  0 siblings, 1 reply; 2+ messages in thread
From: Philippe Waroquiers @ 2019-12-01 19:10 UTC (permalink / raw)
  To: gdb-patches; +Cc: Philippe Waroquiers

A symbol not found inserted in the cache has a xstrdup-ed name
that must be freed, but only the struct block_symbol_cache is freed.
Add a function destroy_block_symbol_cache that clears all slots
before releasing the cache.

YYYY-MM-DD  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
	* symtab.c (symbol_cache_clear_slot):  Move close to cleared type.
	(destroy_block_symbol_cache): New function.
	(symbol_cache:~symbol_cache) Call destroy_block_symbol_cache.
	(resize_symbol_cache): Likewise.
---
 gdb/symtab.c | 41 +++++++++++++++++++++++++++--------------
 1 file changed, 27 insertions(+), 14 deletions(-)

diff --git a/gdb/symtab.c b/gdb/symtab.c
index 894a323003..5c33fbf9ab 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -183,6 +183,16 @@ struct symbol_cache_slot
   } value;
 };
 
+/* Clear out SLOT.  */
+
+static void
+symbol_cache_clear_slot (struct symbol_cache_slot *slot)
+{
+  if (slot->state == SYMBOL_SLOT_NOT_FOUND)
+    xfree (slot->value.not_found.name);
+  slot->state = SYMBOL_SLOT_UNUSED;
+}
+
 /* Symbols don't specify global vs static block.
    So keep them in separate caches.  */
 
@@ -201,6 +211,19 @@ struct block_symbol_cache
   struct symbol_cache_slot symbols[1];
 };
 
+/* Clear all slots of BSC and free BSC.  */
+
+static void
+destroy_block_symbol_cache (struct block_symbol_cache *bsc)
+{
+  if (bsc != nullptr)
+    {
+      for (unsigned int i = 0; i < bsc->size; i++)
+	symbol_cache_clear_slot (&bsc->symbols[i]);
+      xfree (bsc);
+    }
+}
+
 /* The symbol cache.
 
    Searching for symbols in the static and global blocks over multiple objfiles
@@ -217,8 +240,8 @@ struct symbol_cache
 
   ~symbol_cache ()
   {
-    xfree (global_symbols);
-    xfree (static_symbols);
+    destroy_block_symbol_cache (global_symbols);
+    destroy_block_symbol_cache (static_symbols);
   }
 
   struct block_symbol_cache *global_symbols = nullptr;
@@ -1234,8 +1257,8 @@ resize_symbol_cache (struct symbol_cache *cache, unsigned int new_size)
 	  && new_size == 0))
     return;
 
-  xfree (cache->global_symbols);
-  xfree (cache->static_symbols);
+  destroy_block_symbol_cache (cache->global_symbols);
+  destroy_block_symbol_cache (cache->static_symbols);
 
   if (new_size == 0)
     {
@@ -1373,16 +1396,6 @@ symbol_cache_lookup (struct symbol_cache *cache,
   return {};
 }
 
-/* Clear out SLOT.  */
-
-static void
-symbol_cache_clear_slot (struct symbol_cache_slot *slot)
-{
-  if (slot->state == SYMBOL_SLOT_NOT_FOUND)
-    xfree (slot->value.not_found.name);
-  slot->state = SYMBOL_SLOT_UNUSED;
-}
-
 /* Mark SYMBOL as found in SLOT.
    OBJFILE_CONTEXT is the current objfile when the lookup was done, or NULL
    if it's not needed to distinguish lookups (STATIC_BLOCK).  It is *not*
-- 
2.20.1

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [RFA] Fix leak of symbol name in block_symbol_cache
  2019-12-01 19:10 [RFA] Fix leak of symbol name in block_symbol_cache Philippe Waroquiers
@ 2019-12-02 22:24 ` Tom Tromey
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2019-12-02 22:24 UTC (permalink / raw)
  To: Philippe Waroquiers; +Cc: gdb-patches

>>>>> "Philippe" == Philippe Waroquiers <philippe.waroquiers@skynet.be> writes:

Philippe> A symbol not found inserted in the cache has a xstrdup-ed name
Philippe> that must be freed, but only the struct block_symbol_cache is freed.
Philippe> Add a function destroy_block_symbol_cache that clears all slots
Philippe> before releasing the cache.

Philippe> YYYY-MM-DD  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
Philippe> 	* symtab.c (symbol_cache_clear_slot):  Move close to cleared type.
Philippe> 	(destroy_block_symbol_cache): New function.
Philippe> 	(symbol_cache:~symbol_cache) Call destroy_block_symbol_cache.
Philippe> 	(resize_symbol_cache): Likewise.

Thanks.  This is ok.

It would be nicer if everything here was just using destructors, but the
trailing array in block_symbol_cache gets in the way of that.

Tom

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-12-02 22:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-01 19:10 [RFA] Fix leak of symbol name in block_symbol_cache Philippe Waroquiers
2019-12-02 22:24 ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).