From f3d7fd1475eb1ed2b3a39f988b33db176d4f7419 Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Fri, 12 Feb 2021 11:18:17 -0700 Subject: [PATCH] PR c/99055 - memory leak in warn_parm_array_mismatch gcc/c-family/ChangeLog: PR c/99055 * c-warn.c (warn_parm_array_mismatch): Free strings returned from print_generic_expr_to_str. gcc/ChangeLog: * tree-pretty-print.c (print_generic_expr_to_str): Update comment. --- gcc/c-family/c-warn.c | 20 ++++++++++++++++---- gcc/tree-pretty-print.c | 3 ++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c index e6e28d9..2347e0b 100644 --- a/gcc/c-family/c-warn.c +++ b/gcc/c-family/c-warn.c @@ -3319,6 +3319,19 @@ warn_parm_ptrarray_mismatch (location_t origloc, tree curparms, tree newparms) } } +/* Format EXPR if nonnull and return the formatted string. If EXPR is + null return DFLT. */ + +static inline const char* +expr_to_str (pretty_printer &pp, tree expr, const char *dflt) +{ + if (!expr) + return dflt; + + dump_generic_node (&pp, expr, 0, TDF_VOPS | TDF_MEMSYMS, false); + return pp_formatted_text (&pp); +} + /* Detect and diagnose a mismatch between an attribute access specification on the original declaration of FNDECL and that on the parameters NEWPARMS from its refeclaration. ORIGLOC is the location of the first declaration @@ -3585,10 +3598,9 @@ warn_parm_array_mismatch (location_t origloc, tree fndecl, tree newparms) the same. */ continue; - const char* const newbndstr = - newbnd ? print_generic_expr_to_str (newbnd) : "*"; - const char* const curbndstr = - curbnd ? print_generic_expr_to_str (curbnd) : "*"; + pretty_printer pp1, pp2; + const char* const newbndstr = expr_to_str (pp1, newbnd, "*"); + const char* const curbndstr = expr_to_str (pp2, curbnd, "*"); if (!newpos != !curpos || (newpos && !tree_int_cst_equal (newpos, curpos))) diff --git a/gcc/tree-pretty-print.c b/gcc/tree-pretty-print.c index aabe6bb..986f75d 100644 --- a/gcc/tree-pretty-print.c +++ b/gcc/tree-pretty-print.c @@ -169,7 +169,8 @@ print_generic_expr (FILE *file, tree t, dump_flags_t flags) pp_flush (tree_pp); } -/* Print a single expression T to string, and return it. */ +/* Print a single expression T to string, and return it. The caller + must free the returned memory. */ char * print_generic_expr_to_str (tree t) -- 2.7.4