From e88e8651cf3415ba440ee17eb1b22b7d2e8368be Mon Sep 17 00:00:00 2001 From: Yao Qi Date: Wed, 9 Aug 2017 12:39:16 +0100 Subject: [PATCH] Fix memory leak in cp-support.c The return value of cp_comp_to_string was never freed, creating a sizable memory leak detectable with valgrind. ==21225== 8 bytes in 1 blocks are definitely lost in loss record 4,599 of 10,949^M ==21225== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)^M ==21225== by 0x4C2FDEF: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)^M ==21225== by 0x76CB31: d_growable_string_resize (cp-demangle.c:3963)^M ==21225== by 0x76CB31: d_growable_string_init (cp-demangle.c:3942)^M ==21225== by 0x76CB31: cplus_demangle_print (cp-demangle.c:4308)^M ==21225== by 0x4C9535: cp_comp_to_string(demangle_component*, int) (cp-name-parser.y:1972)^M ==21225== by 0x53E1D4: cp_canonicalize_string_full[abi:cxx11](char const*, char const* (*)(type*, void*), void*) (cp-support.c:530)^M ==21225== by 0x53E360: cp_canonicalize_string_no_typedefs[abi:cxx11](char const*) (cp-support.c:548)^M ==21225== by 0x5D51D2: find_linespec_symbols(linespec_state*, VEC_symtab_ptr*, char const*, VEC_symbolp**, VEC_bound_minimal_symbol_d**) (linespec.c:4030)^M ==21225== by 0x5D6CF6: linespec_parse_basic (linespec.c:1907) ==21279== 32 bytes in 1 blocks are definitely lost in loss record 6,066 of 10,947^M ==21279== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)^M ==21279== by 0x4C2FDEF: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)^M ==21279== by 0x76CB31: d_growable_string_resize (cp-demangle.c:3963)^M ==21279== by 0x76CB31: d_growable_string_init (cp-demangle.c:3942)^M ==21279== by 0x76CB31: cplus_demangle_print (cp-demangle.c:4308)^M ==21279== by 0x4C9535: cp_comp_to_string(demangle_component*, int) (cp-name-parser.y:1972)^M ==21279== by 0x53EF14: cp_canonicalize_string[abi:cxx11](char const*) (cp-support.c:569)^M ==21279== by 0x561B75: dwarf2_canonicalize_name(char const*, dwarf2_cu*, obstack*) [clone .isra.210] (dwarf2read.c:20159) This patch fixes the leak. It is a regression by 2f408ecb. gdb: 2017-08-09 Alex Lindsay Yao Qi * cp-support.c (cp_canonicalize_string_full): Use gdb::unique_xmalloc_ptr. (cp_canonicalize_string): Likewise. --- gdb/ChangeLog | 7 +++++++ gdb/cp-support.c | 13 +++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 97c39d7bcde..209d0b6da6e 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,10 @@ +2017-08-09 Alex Lindsay + Yao Qi + + * cp-support.c (cp_canonicalize_string_full): Use + gdb::unique_xmalloc_ptr. + (cp_canonicalize_string): Likewise. + 2017-08-09 Yao Qi * features/Makefile (WHICH): Remove i386/ non-linux stuff. diff --git a/gdb/cp-support.c b/gdb/cp-support.c index df9a563508c..f6557ab310f 100644 --- a/gdb/cp-support.c +++ b/gdb/cp-support.c @@ -527,9 +527,11 @@ cp_canonicalize_string_full (const char *string, replace_typedefs (info.get (), info->tree, finder, data); /* Convert the tree back into a string. */ - ret = cp_comp_to_string (info->tree, estimated_len); - gdb_assert (!ret.empty ()); + gdb::unique_xmalloc_ptr us (cp_comp_to_string (info->tree, + estimated_len)); + gdb_assert (us); + ret = us.get (); /* Finally, compare the original string with the computed name, returning NULL if they are the same. */ if (ret == string) @@ -566,15 +568,18 @@ cp_canonicalize_string (const char *string) return std::string (); estimated_len = strlen (string) * 2; - std::string ret = cp_comp_to_string (info->tree, estimated_len); + gdb::unique_xmalloc_ptr us (cp_comp_to_string (info->tree, + estimated_len)); - if (ret.empty ()) + if (!us) { warning (_("internal error: string \"%s\" failed to be canonicalized"), string); return std::string (); } + std::string ret (us.get ()); + if (ret == string) return std::string (); -- 2.30.2