Re: libiberty/cplus-dem.c, ada-demangle: plug memory leak.

Michael Snyder
2011-03-03T22:00:11Z

[-- Attachment #1: Type: text/plain, Size: 712 bytes --]

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?

[-- Attachment #2: cplus-dem2.txt --]
[-- Type: text/plain, Size: 1043 bytes --]

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);