[RFA] Fix leak detected in GDB test gdb.base/macscp.exp

Message ID 87zhru6lxl.fsf@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Jan. 21, 2019, 3:59 p.m. UTC
  >>>>> "Philippe" == Philippe Waroquiers <philippe.waroquiers@skynet.be> writes:

Philippe> libiberty/ChangeLog
Philippe> 2019-01-19  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
Philippe> 	* splay-tree.c (splay_tree_remove): Call sp->delete_key.
Philippe> 	(splay_tree_new_typed_alloc): Update comment.

Like I mentioned before, I didn't see this before sending my own patch
to gcc.

I'm checking in the appended to binutils-gdb now.

I looked at all splay_tree_new* calls in binutils-gdb and the only one
impacted is the one in gdb that causes the leak.

Tom

commit 33ee267e434ae4b7acb44537f8be5a6663f3b70f
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Jan 18 14:19:55 2019 -0700

    Fix leak in splay-tree
    
    Philippe Waroquiers noticed a memory leak in gdb, which he tracked
    down to a bug in splay-tree.  splay_tree_remove does not call the
    `delete_key' function when it removes the old node; but it should.
    
    I looked at every splay tree in GCC and there is only one that passes
    a non-NULL delete function -- the one in lto.c.  That file does not
    call splay_tree_remove.  So, I think this is safe to check in.
    
    I re-ran the LTO tests to double check.
    
    libiberty/
            * splay-tree.c (splay_tree_remove): Delete the key if necessary.
  

Patch

diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog
index bcc0227bdd8..1eb25f928f2 100644
--- a/libiberty/ChangeLog
+++ b/libiberty/ChangeLog
@@ -1,3 +1,7 @@ 
+2019-01-18  Tom Tromey  <tom@tromey.com>
+
+	* splay-tree.c (splay_tree_remove): Delete the key if necessary.
+
 2019-01-14  Tom Honermann  <tom@honermann.net>
 
 	* cp-demangle.c (cplus_demangle_builtin_types)
diff --git a/libiberty/splay-tree.c b/libiberty/splay-tree.c
index 920e68db2cb..21d23c38dfc 100644
--- a/libiberty/splay-tree.c
+++ b/libiberty/splay-tree.c
@@ -425,6 +425,8 @@  splay_tree_remove (splay_tree sp, splay_tree_key key)
       right = sp->root->right;
 
       /* Delete the root node itself.  */
+      if (sp->delete_key)
+	(*sp->delete_key) (sp->root->key);
       if (sp->delete_value)
 	(*sp->delete_value) (sp->root->value);
       (*sp->deallocate) (sp->root, sp->allocate_data);