This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [PATCH] Fix memory leak in python.c:do_start_initialization
- From: Pedro Alves <palves at redhat dot com>
- To: Philipp Rudo <prudo at linux dot vnet dot ibm dot com>, gdb-patches at sourceware dot org
- Date: Wed, 22 Mar 2017 15:19:16 +0000
- Subject: Re: [PATCH] Fix memory leak in python.c:do_start_initialization
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=palves at redhat dot com
- Dkim-filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 1695619D228
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1695619D228
- References: <20170322131132.98976-1-prudo@linux.vnet.ibm.com>
Hi Philipp,
Thanks.
On 03/22/2017 01:11 PM, Philipp Rudo wrote:
> diff --git a/gdb/python/python.c b/gdb/python/python.c
> index 73fb3d0..6b16613 100644
> --- a/gdb/python/python.c
> +++ b/gdb/python/python.c
> @@ -1535,7 +1535,7 @@ extern initialize_file_ftype _initialize_python;
> static bool
> do_start_initialization ()
> {
> - char *progname;
> + char *progname, *libdir;
> #ifdef IS_PY3K
> int i;
> size_t progsize, count;
> @@ -1550,8 +1550,10 @@ do_start_initialization ()
> /foo/bin/python
> /foo/lib/pythonX.Y/...
> This must be done before calling Py_Initialize. */
> - progname = concat (ldirname (python_libdir), SLASH_STRING, "bin",
> + libdir = ldirname (python_libdir);
> + progname = concat (libdir, SLASH_STRING, "bin",
> SLASH_STRING, "python", (char *) NULL);
> + xfree (libdir);
Let's restrict the new variable to the #if block that needs it.
I.e., declare the variable where is initialized, like:
const char *libdir = ldirname (python_libdir);
progname = concat (libdir, SLASH_STRING, "bin",
OK with that change. Please push.
Note, you could have used reconcat instead of concat, avoiding the
xfree call, and maybe one reallocation, but that's hardly an
issue here.
Perhaps better overall would be to make ldirname return a std::string
and eliminate these leaks "by design". It'd get rid of several
make_cleanup calls throughout too. I'll give that a quick try.
Thanks,
Pedro Alves