From patchwork Sun Sep 8 07:49:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Waroquiers X-Patchwork-Id: 34447 Received: (qmail 81183 invoked by alias); 8 Sep 2019 07:49:33 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 81174 invoked by uid 89); 8 Sep 2019 07:49:32 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.1 spammy=UD:main.c, mainc, main.c, leak X-HELO: mailsec111.isp.belgacom.be Received: from mailsec111.isp.belgacom.be (HELO mailsec111.isp.belgacom.be) (195.238.20.107) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 08 Sep 2019 07:49:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skynet.be; i=@skynet.be; q=dns/txt; s=securemail; t=1567928970; x=1599464970; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=m8j6WxV2BuhvN/Oisgid4bIZ8VxXTCP5EBTfrWdiEgw=; b=0OREoHO/9Zgo/qnIIe38NAQnhJQl1xHidcWqrXA9fSP+HUZ83Hh8Cv08 eKl64WRCf7iCgcWhZ3cQ1Qumddv4Fw==; Received: from 255.38-242-81.adsl-dyn.isp.belgacom.be (HELO md.home) ([81.242.38.255]) by relay.skynet.be with ESMTP/TLS/DHE-RSA-AES128-GCM-SHA256; 08 Sep 2019 09:49:28 +0200 From: Philippe Waroquiers To: gdb-patches@sourceware.org Cc: Philippe Waroquiers Subject: [RFA] Fix leak detected in python.c initialization code. Date: Sun, 8 Sep 2019 09:49:22 +0200 Message-Id: <20190908074922.29305-1-philippe.waroquiers@skynet.be> MIME-Version: 1.0 X-IsSubscribed: yes Valgrind reports the below leak. Make the variable progname_copy static, so that Valgrind continues to find a pointer to the memory given to Python. Note that the comment in do_start_initialization and the Python documentation indicates that the progname given to Py_SetProgramName cannot be freed. However, in Python 3.7.4, Py_SetProgramName does: void Py_SetProgramName(const wchar_t *program_name) { ... PyMem_RawFree(_Py_path_config.program_name); _Py_path_config.program_name = _PyMem_RawWcsdup(program_name); So, it looks like 3.7.4 Python duplicates its argument, which explains the leak found by Valgrind. It looks better to respect the doc and not have GDB freeing the string given to Py_SetProgramName, and avoid the leak error by declaring the progname_copy static. This will work with Python versions that really use this string without duplicating it, and avoids a leak report for Python version that duplicates it. ==4023== 200 bytes in 1 blocks are definitely lost in loss record 4,545 of 7,116^M ==4023== at 0x4C29F33: malloc (vg_replace_malloc.c:307)^M ==4023== by 0x446D27: xmalloc (alloc.c:60)^M ==4023== by 0x657C77: do_start_initialization (python.c:1610)^M ==4023== by 0x657C77: _initialize_python() (python.c:1823)^M ==4023== by 0x75FE24: initialize_all_files() (init.c:231)^M ==4023== by 0x708A94: gdb_init(char*) (top.c:2242)^M ==4023== by 0x5E7460: captured_main_1 (main.c:857)^M ==4023== by 0x5E7460: captured_main (main.c:1161)^M ==4023== by 0x5E7460: gdb_main(captured_main_args*) (main.c:1186)^M ==4023== by 0x4122D4: main (gdb.c:32)^M gdb/ChangeLog YYYY-MM-DD Philippe Waroquiers * python/python.c (do_start_initialization): Make progname_copy static, to avoid a leak report. --- gdb/python/python.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdb/python/python.c b/gdb/python/python.c index b309ae91ba..1d9178ebcc 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1590,7 +1590,7 @@ do_start_initialization () { #ifdef IS_PY3K size_t progsize, count; - wchar_t *progname_copy; + static wchar_t *progname_copy; #endif #ifdef WITH_PYTHON_PATH