Fix memory leak in cp-support.c
authorYao Qi <yao.qi@linaro.org>
Wed, 9 Aug 2017 11:39:16 +0000 (12:39 +0100)
committerYao Qi <yao.qi@linaro.org>
Wed, 9 Aug 2017 11:39:16 +0000 (12:39 +0100)
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  <alexlindsay239@gmail.com>
    Yao Qi  <yao.qi@linaro.org>

* cp-support.c (cp_canonicalize_string_full): Use
gdb::unique_xmalloc_ptr<char>.
(cp_canonicalize_string): Likewise.

gdb/ChangeLog
gdb/cp-support.c

index 97c39d7..209d0b6 100644 (file)
@@ -1,3 +1,10 @@
+2017-08-09  Alex Lindsay  <alexlindsay239@gmail.com>
+           Yao Qi  <yao.qi@linaro.org>
+
+       * cp-support.c (cp_canonicalize_string_full): Use
+       gdb::unique_xmalloc_ptr<char>.
+       (cp_canonicalize_string): Likewise.
+
 2017-08-09  Yao Qi  <yao.qi@linaro.org>
 
        * features/Makefile (WHICH): Remove i386/ non-linux stuff.
index df9a563..f6557ab 100644 (file)
@@ -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<char> 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<char> 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 ();