This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: libiberty/cplus-dem.c, ada-demangle: plug memory leak.
- From: Michael Snyder <msnyder at vmware dot com>
- To: Jakub Jelinek <jakub at redhat dot com>
- Cc: "dj at redhat dot com" <dj at redhat dot com>, "gdb-patches at sourceware dot org" <gdb-patches at sourceware dot org>, "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 03 Mar 2011 13:59:57 -0800
- Subject: Re: libiberty/cplus-dem.c, ada-demangle: plug memory leak.
- References: <4D70061C.10904@vmware.com> <20110303213008.GY30899@tyan-ft48-01.lab.bos.redhat.com>
Jakub Jelinek wrote:
On Thu, Mar 03, 2011 at 01:20:28PM -0800, Michael Snyder wrote:
2011-03-03 Michael Snyder <msnyder@vmware.com>
* libiberty/cplus-dem.c (ada_demangle): Stop memory leak.
Also fix a one line indent problem.
No libiberty/ in libiberty/ChangeLog.
@@ -1129,10 +1129,11 @@ ada_demangle (const char *mangled, int o
unknown:
len0 = strlen (mangled);
+ xfree (demangled);
demangled = XNEWVEC (char, len0 + 3);
xfree isn't ever used in libiberty/*, use either free, or
XDELETE/XDELETEVEC. In fact, it seems to be defined only in gdb,
making cplus-dem.c dependent on gdb is obviously a wrong thing.
Thanks for the review.
How's this?
2011-03-03 Michael Snyder <msnyder@vmware.com>
* cplus-dem.c (ada_demangle): Stop memory leak.
Also fix a one line indent problem.
Index: cplus-dem.c
===================================================================
RCS file: /cvs/src/src/libiberty/cplus-dem.c,v
retrieving revision 1.52
diff -u -p -u -p -r1.52 cplus-dem.c
--- cplus-dem.c 3 Jan 2011 21:05:58 -0000 1.52
+++ cplus-dem.c 3 Mar 2011 21:59:08 -0000
@@ -883,7 +883,7 @@ ada_demangle (const char *mangled, int o
int len0;
const char* p;
char *d;
- char *demangled;
+ char *demangled = NULL;
/* Discard leading _ada_, which is used for library level subprograms. */
if (strncmp (mangled, "_ada_", 5) == 0)
@@ -1129,10 +1129,12 @@ ada_demangle (const char *mangled, int o
unknown:
len0 = strlen (mangled);
+ if (demangled != NULL)
+ free (demangled);
demangled = XNEWVEC (char, len0 + 3);
if (mangled[0] == '<')
- strcpy (demangled, mangled);
+ strcpy (demangled, mangled);
else
sprintf (demangled, "<%s>", mangled);