Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Fix memory leak in cp-support.c (cp_canonicalize_string)
@ 2017-08-07 20:18 Alex Lindsay
  2017-08-09 11:40 ` Yao Qi
  0 siblings, 1 reply; 8+ messages in thread
From: Alex Lindsay @ 2017-08-07 20:18 UTC (permalink / raw)
  To: gdb-patches

Formerly, in cp_canonicalize_string in cp-support.c, the return value of
cp_comp_to_string was never freed, creating a sizable memory leak detectable
with valgrind. This patch fixes the leak. However, a longer term solution
would be to change the return type of cp_comp_to_string to
gdb::unique_xmalloc_ptr<char>.
---
 gdb/ChangeLog    | 5 +++++
 gdb/cp-support.c | 4 +++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 92c573d76b..e0c43c6185 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2017-08-07 Alex Lindsay <alexlindsay239@gmail.com>
+
+	* cp-support.c (cp_canonicalize_string): Free return value of
+	cp_comp_to_string
+
 2017-08-03  Tom Tromey  <tom@tromey.com>
 
 	* utils.c (make_cleanup_freeargv, do_freeargv, gdb_buildargv):
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index df9a563508..ffbfd62ee0 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -566,7 +566,9 @@ 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);
+  char *ret_char = cp_comp_to_string (info->tree, estimated_len);
+  std::string ret = ret_char;
+  xfree (ret_char);
 
   if (ret.empty ())
     {
-- 
2.11.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2017-08-09 14:10 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-07 20:18 [PATCH] Fix memory leak in cp-support.c (cp_canonicalize_string) Alex Lindsay
2017-08-09 11:40 ` Yao Qi
2017-08-09 12:09   ` Pedro Alves
2017-08-09 12:31     ` Pedro Alves
2017-08-09 13:27       ` Pedro Alves
2017-08-09 13:41       ` Yao Qi
2017-08-09 14:10         ` Pedro Alves
2017-08-09 13:24   ` Alex Lindsay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox