[22/27] Fix memory leak in mdebugread.c

Message ID 20230120214618.3236224-23-tom@tromey.com
State New
Headers
Series C++-ify struct block |

Commit Message

Tom Tromey Jan. 20, 2023, 9:46 p.m. UTC
  mdebugread.c allocates blocks on the heap.  However, this is a memory
leak if the corresponding objfile is ever destroyed.

This patch changes this code to use allocate_block instead, fixing a
FIXME from 2003.

I don't know how to test this patch.
---
 gdb/mdebugread.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
  

Patch

diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c
index 7fa25ae3183..b6a0b3c07df 100644
--- a/gdb/mdebugread.c
+++ b/gdb/mdebugread.c
@@ -236,7 +236,8 @@  static struct type *new_type (char *);
 
 enum block_type { FUNCTION_BLOCK, NON_FUNCTION_BLOCK };
 
-static struct block *new_block (enum block_type, enum language);
+static struct block *new_block (struct objfile *objfile,
+				enum block_type, enum language);
 
 static struct compunit_symtab *new_symtab (const char *, int, struct objfile *);
 
@@ -794,7 +795,7 @@  parse_symbol (SYMR *sh, union aux_ext *ax, char *ext_sh, int bigend,
 	s->type ()->set_is_prototyped (true);
 
       /* Create and enter a new lexical context.  */
-      b = new_block (FUNCTION_BLOCK, s->language ());
+      b = new_block (objfile, FUNCTION_BLOCK, s->language ());
       s->set_value_block (b);
       b->set_function (s);
       b->set_start (sh->value);
@@ -1125,7 +1126,7 @@  parse_symbol (SYMR *sh, union aux_ext *ax, char *ext_sh, int bigend,
 	}
 
       top_stack->blocktype = stBlock;
-      b = new_block (NON_FUNCTION_BLOCK, psymtab_language);
+      b = new_block (objfile, NON_FUNCTION_BLOCK, psymtab_language);
       b->set_start (sh->value + top_stack->procadr);
       b->set_superblock (top_stack->cur_block);
       top_stack->cur_block = b;
@@ -4622,8 +4623,8 @@  new_symtab (const char *name, int maxlines, struct objfile *objfile)
 
   /* All symtabs must have at least two blocks.  */
   bv = new_bvect (2);
-  bv->set_block (GLOBAL_BLOCK, new_block (NON_FUNCTION_BLOCK, lang));
-  bv->set_block (STATIC_BLOCK, new_block (NON_FUNCTION_BLOCK, lang));
+  bv->set_block (GLOBAL_BLOCK, new_block (objfile, NON_FUNCTION_BLOCK, lang));
+  bv->set_block (STATIC_BLOCK, new_block (objfile, NON_FUNCTION_BLOCK, lang));
   bv->static_block ()->set_superblock (bv->global_block ());
   cust->set_blockvector (bv);
 
@@ -4710,12 +4711,10 @@  new_bvect (int nblocks)
    linearly; otherwise, store them hashed.  */
 
 static struct block *
-new_block (enum block_type type, enum language language)
+new_block (struct objfile *objfile, enum block_type type,
+	   enum language language)
 {
-  /* FIXME: carlton/2003-09-11: This should use allocate_block to
-     allocate the block.  Which, in turn, suggests that the block
-     should be allocated on an obstack.  */
-  struct block *retval = XCNEW (struct block);
+  struct block *retval = allocate_block (&objfile->objfile_obstack);
 
   if (type == FUNCTION_BLOCK)
     retval->set_multidict (mdict_create_linear_expandable (language));