From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 116406 invoked by alias); 22 Mar 2017 15:19:21 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 116027 invoked by uid 89); 22 Mar 2017 15:19:20 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 22 Mar 2017 15:19:19 +0000 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1695619D228; Wed, 22 Mar 2017 15:19:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1695619D228 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@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 1695619D228 Received: from [127.0.0.1] (ovpn04.gateway.prod.ext.phx2.redhat.com [10.5.9.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7240F852D0; Wed, 22 Mar 2017 15:19:19 +0000 (UTC) Subject: Re: [PATCH] Fix memory leak in python.c:do_start_initialization To: Philipp Rudo , gdb-patches@sourceware.org References: <20170322131132.98976-1-prudo@linux.vnet.ibm.com> From: Pedro Alves Message-ID: Date: Wed, 22 Mar 2017 15:19:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <20170322131132.98976-1-prudo@linux.vnet.ibm.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-SW-Source: 2017-03/txt/msg00399.txt.bz2 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