vtv: fix memory leak by freeing unnecessary variable

Currently, vtable_find_or_create_map_decl, given a class decl,
calls get_mangled_vtable_map_var_name (from mangle.cc) to:

  (1) create a VTV map entry for the given class; and
  (2) return the expected DECL_ASSEMBLER_NAME of its VTV tracker.

Creating this decl is only needed once for each class,
hence the 'if' clause on vtable-class-hierarchy.cc:1120.

On this patch I note that if we don't need to create the decl, we
should dispose of the xmalloc'd 'var_name' that won't be stored.

Since there is only one listed maintainer for libvtv, who doesn't
seem active in recent years, is it possible for global reviewers
to take a look at this patch?

I hope the logic is straightforward enough: both the caller and
callee mentioned above have a single caller, and this variable is
only used inside the 'if' condition.

For testing, one might bootstrap gcc with --enable-vtable-verify,
and notice that the new 'free' line is hit many times.

Thank you very much for your time,

gcc/cp/ChangeLog:

        * vtable-class-hierarchy.cc (vtable_find_or_create_map_decl):
        free VAR_NAME if we aren't going to use it.

Signed-off-by: Léo Hardt <[email protected]>
---
 gcc/cp/vtable-class-hierarchy.cc | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/gcc/cp/vtable-class-hierarchy.cc b/gcc/cp/vtable-class-hierarchy.cc
index 2e52ea3c4a0..6692516551b 100644
--- a/gcc/cp/vtable-class-hierarchy.cc
+++ b/gcc/cp/vtable-class-hierarchy.cc
@@ -1256,6 +1256,8 @@ vtable_find_or_create_map_decl (tree base_type)
find_or_create_vtbl_map_node (TYPE_MAIN_VARIANT (base_type));
       if (vtable_map_node->vtbl_map_decl == NULL_TREE)
         vtable_map_node->vtbl_map_decl = var_decl;
+    } else {
+      free (var_name);
     }

   gcc_assert (vtable_map_node);
--
2.39.5


Reply via email to