Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@polymtl.ca>
Subject: [PATCH] python: Fix memleak in do_start_initialization
Date: Sun, 26 Nov 2017 03:53:00 -0000	[thread overview]
Message-ID: <20171126035308.12274-1-simon.marchi@polymtl.ca> (raw)

While playing with valgrind, I noticed that the progname variable in
do_start_initialization is not being freed (concat returns a malloc'ed
string).  Use a unique_xmalloc_ptr to manage it.

gdb/ChangeLog:

	* python/python.c (do_start_initialization): Change progname
	type to gdb::unique_xmalloc_ptr.
---
 gdb/python/python.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/gdb/python/python.c b/gdb/python/python.c
index 5f152611e8..f17ac36569 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1658,7 +1658,7 @@ finalize_python (void *ignore)
 static bool
 do_start_initialization ()
 {
-  char *progname;
+  gdb::unique_xmalloc_ptr<char> progname;
 #ifdef IS_PY3K
   int i;
   size_t progsize, count;
@@ -1672,19 +1672,19 @@ do_start_initialization ()
      /foo/bin/python
      /foo/lib/pythonX.Y/...
      This must be done before calling Py_Initialize.  */
-  progname = concat (ldirname (python_libdir).c_str (), SLASH_STRING, "bin",
-		     SLASH_STRING, "python", (char *) NULL);
+  progname.reset (concat (ldirname (python_libdir).c_str (), SLASH_STRING,
+			  "bin", SLASH_STRING, "python", (char *) NULL));
 #ifdef IS_PY3K
   std::string oldloc = setlocale (LC_ALL, NULL);
   setlocale (LC_ALL, "");
-  progsize = strlen (progname);
+  progsize = strlen (progname.get ());
   progname_copy = (wchar_t *) PyMem_Malloc ((progsize + 1) * sizeof (wchar_t));
   if (!progname_copy)
     {
       fprintf (stderr, "out of memory\n");
       return false;
     }
-  count = mbstowcs (progname_copy, progname, progsize + 1);
+  count = mbstowcs (progname_copy, progname.get (), progsize + 1);
   if (count == (size_t) -1)
     {
       fprintf (stderr, "Could not convert python path to string\n");
-- 
2.15.0


             reply	other threads:[~2017-11-26  3:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-26  3:53 Simon Marchi [this message]
2017-11-26 19:00 ` Tom Tromey
2017-11-26 20:56   ` Simon Marchi
2017-11-26 21:55     ` Tom Tromey
2017-11-27  0:36       ` 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=20171126035308.12274-1-simon.marchi@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --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