From a3f3eec079677bc6042dc7b00e8f85f644b97d57 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Tue, 1 Nov 2016 23:13:10 +0000 Subject: [PATCH] libiberty: Fix memory leak in ada_demangle when symbol cannot be demangled. When a symbol cannot be demangled in ada_demangle a new demangled VEC will be allocated without deleting the demangled VEC already in use. Running testsuite/test-demangle under valgrind will show the leak for this entry in testsuite/demangle-expected: # Elaborated flag (not demangled) --format=gnat x_E 11 bytes in 1 blocks are definitely lost in loss record 1 of 1 at 0x4C27BE3: malloc (vg_replace_malloc.c:299) by 0x413FE7: xmalloc (xmalloc.c:148) by 0x4025EC: ada_demangle (cplus-dem.c:930) by 0x402C59: cplus_demangle (cplus-dem.c:892) by 0x400FEC: main (test-demangle.c:317) libiberty/ChangeLog: * cplus-dem.c (ada_demangle): Initialize demangled to NULL and XDELETEVEC demangled when unknown. From-SVN: r241760 --- libiberty/ChangeLog | 5 +++++ libiberty/cplus-dem.c | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog index 5934bc1fa8a..73cbcc41c60 100644 --- a/libiberty/ChangeLog +++ b/libiberty/ChangeLog @@ -1,3 +1,8 @@ +2016-10-31 Mark Wielaard + + * cplus-dem.c (ada_demangle): Initialize demangled to NULL and + XDELETEVEC demangled when unknown. + 2016-09-19 Andrew Stubbs * pex-win32.c (argv_to_cmdline): Quote zero-length parameters. diff --git a/libiberty/cplus-dem.c b/libiberty/cplus-dem.c index f95405062c9..7f63397f2d4 100644 --- a/libiberty/cplus-dem.c +++ b/libiberty/cplus-dem.c @@ -911,7 +911,7 @@ ada_demangle (const char *mangled, int option ATTRIBUTE_UNUSED) 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) @@ -1156,6 +1156,7 @@ ada_demangle (const char *mangled, int option ATTRIBUTE_UNUSED) return demangled; unknown: + XDELETEVEC (demangled); len0 = strlen (mangled); demangled = XNEWVEC (char, len0 + 3); -- 2.30.2