https://gcc.gnu.org/g:d77f073ce66cedbcbb22357c49b9ef19e1b61a43

commit r15-4048-gd77f073ce66cedbcbb22357c49b9ef19e1b61a43
Author: Jason Merrill <ja...@redhat.com>
Date:   Thu Oct 3 16:31:00 2024 -0400

    c++: free garbage vec in coerce_template_parms
    
    coerce_template_parms can create two different vecs for the inner template
    arguments, new_inner_args and (potentially) the result of
    expand_template_argument_pack.  One or the other, or possibly both, end up
    being garbage: in the typical case, the expanded vec is garbage because it's
    only used as the source for convert_template_argument.  In some dependent
    cases, the new vec is garbage because we decide to return the original args
    instead.  In these cases, ggc_free the garbage vec to reduce the memory
    overhead of overload resolution.
    
    gcc/cp/ChangeLog:
    
            * pt.cc (coerce_template_parms): Free garbage vecs.
    
    Co-authored-by: Richard Biener <rguent...@suse.de>

Diff:
---
 gcc/cp/pt.cc | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 20affcd65a27..4ceae1d38de1 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -9275,6 +9275,7 @@ coerce_template_parms (tree parms,
            {
              /* We don't know how many args we have yet, just use the
                 unconverted (and still packed) ones for now.  */
+             ggc_free (new_inner_args);
              new_inner_args = orig_inner_args;
              arg_idx = nargs;
              break;
@@ -9329,7 +9330,8 @@ coerce_template_parms (tree parms,
                  = make_pack_expansion (conv, complain);
 
               /* We don't know how many args we have yet, just
-                 use the unconverted ones for now.  */
+                use the unconverted (but unpacked) ones for now.  */
+             ggc_free (new_inner_args);
               new_inner_args = inner_args;
              arg_idx = nargs;
               break;
@@ -9442,6 +9444,12 @@ coerce_template_parms (tree parms,
     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
                                         TREE_VEC_LENGTH (new_inner_args));
 
+  /* If we expanded packs in inner_args and aren't returning it now, the
+     expanded vec is garbage.  */
+  if (inner_args != new_inner_args
+      && inner_args != orig_inner_args)
+    ggc_free (inner_args);
+
   return return_full_args ? new_args : new_inner_args;
 }

Reply via email to