public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [RFA] Fix a memory leak in py-param.c
Date: Wed, 20 Jun 2018 21:38:00 -0000	[thread overview]
Message-ID: <20180620213836.3980-1-tom@tromey.com> (raw)

Mark Wielaard pointed out this memory leak to me:

    ==17633== 775 bytes in 1 blocks are definitely lost in loss record 13,346 of 13,967
    ==17633==    at 0x4C2DB6B: malloc (vg_replace_malloc.c:299)
    ==17633==    by 0x6652B7: xmalloc (common-utils.c:45)
    ==17633==    by 0xC4C889: xstrdup (xstrdup.c:34)
    ==17633==    by 0x5A71FD: unicode_to_encoded_string(_object*, char const*) (py-utils.c:81)
    ==17633==    by 0x5A73EB: python_string_to_host_string(_object*) (py-utils.c:158)
    ==17633==    by 0x59CC6C: get_doc_string(_object*, _object*) (py-param.c:334)
    ==17633==    by 0x59D2AA: parmpy_init(_object*, _object*, _object*) (py-param.c:728)

The bug here is that parmpy_init is written as though
add_setshow_generic takes ownership of its doc-string arguments.
However, it does not.  This patch fixes the bug in a straightforward
way and also applies some missing constification to make the problem
more apparent.

Tested on x86-64 Fedora 26.

gdb/ChangeLog
2018-06-20  Tom Tromey  <tom@tromey.com>

	* python/py-param.c (add_setshow_generic): Make parameters const.
	(parmpy_init): Update.
---
 gdb/ChangeLog         |  5 +++++
 gdb/python/py-param.c | 20 +++++++++-----------
 2 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/gdb/python/py-param.c b/gdb/python/py-param.c
index ef5c91b9ba1..0f0214befe0 100644
--- a/gdb/python/py-param.c
+++ b/gdb/python/py-param.c
@@ -465,8 +465,9 @@ get_show_value (struct ui_file *file, int from_tty,
    function.  */
 static void
 add_setshow_generic (int parmclass, enum command_class cmdclass,
-		     char *cmd_name, parmpy_object *self,
-		     char *set_doc, char *show_doc, char *help_doc,
+		     const char *cmd_name, parmpy_object *self,
+		     const char *set_doc, const char *show_doc,
+		     const char *help_doc,
 		     struct cmd_list_element **set_list,
 		     struct cmd_list_element **show_list)
 {
@@ -662,7 +663,7 @@ parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
 {
   parmpy_object *obj = (parmpy_object *) self;
   const char *name;
-  char *set_doc, *show_doc, *doc;
+  gdb::unique_xmalloc_ptr<char> set_doc, show_doc, doc;
   char *cmd_name;
   int parmclass, cmdtype;
   PyObject *enum_values = NULL;
@@ -723,9 +724,9 @@ parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
   if (! cmd_name)
     return -1;
 
-  set_doc = get_doc_string (self, set_doc_cst).release ();
-  show_doc = get_doc_string (self, show_doc_cst).release ();
-  doc = get_doc_string (self, gdbpy_doc_cst).release ();
+  set_doc = get_doc_string (self, set_doc_cst);
+  show_doc = get_doc_string (self, show_doc_cst);
+  doc = get_doc_string (self, gdbpy_doc_cst);
 
   Py_INCREF (self);
 
@@ -733,15 +734,12 @@ parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
     {
       add_setshow_generic (parmclass, (enum command_class) cmdtype,
 			   cmd_name, obj,
-			   set_doc, show_doc,
-			   doc, set_list, show_list);
+			   set_doc.get (), show_doc.get (),
+			   doc.get (), set_list, show_list);
     }
   CATCH (except, RETURN_MASK_ALL)
     {
       xfree (cmd_name);
-      xfree (set_doc);
-      xfree (show_doc);
-      xfree (doc);
       Py_DECREF (self);
       PyErr_Format (except.reason == RETURN_QUIT
 		    ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
-- 
2.13.6

             reply	other threads:[~2018-06-20 21:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-20 21:38 Tom Tromey [this message]
2018-06-21  1:39 ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180620213836.3980-1-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).