public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Fix leak detected in python.c initialization code.
@ 2019-09-09 21:52 Philippe Waroquiers
  0 siblings, 0 replies; only message in thread
From: Philippe Waroquiers @ 2019-09-09 21:52 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e4df087431f10f6d9079d8f0c78ad0414e814b92

commit e4df087431f10f6d9079d8f0c78ad0414e814b92
Author: Philippe Waroquiers <philippe.waroquiers@skynet.be>
Date:   Sat Sep 7 20:54:44 2019 +0200

    Fix leak detected in python.c initialization code.
    
    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
    2019-09-09  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
    
    	* python/python.c (do_start_initialization): Make progname_copy static,
    	to avoid a leak report.

Diff:
---
 gdb/ChangeLog       | 5 +++++
 gdb/python/python.c | 9 ++++++++-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index af1608d..eb6d062 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2019-09-09  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
+
+	* python/python.c (do_start_initialization): Make progname_copy static,
+	to avoid a leak report.
+
 2019-09-08  Tom Tromey  <tom@tromey.com>
 
 	* tui/tui-wingeneral.c (box_win): Truncate long window titles.
diff --git a/gdb/python/python.c b/gdb/python/python.c
index b309ae9..9c49151 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1590,7 +1590,14 @@ do_start_initialization ()
 {
 #ifdef IS_PY3K
   size_t progsize, count;
-  wchar_t *progname_copy;
+  /* Python documentation indicates that the memory given
+     to Py_SetProgramName cannot be freed.  However, it seems that
+     at least Python 3.7.4 Py_SetProgramName takes a copy of the
+     given program_name.  Making progname_copy static and not release
+     the memory avoids a leak report for Python versions that duplicate
+     program_name, and respect the requirement of Py_SetProgramName
+     for Python versions that do not duplicate program_name.  */
+  static wchar_t *progname_copy;
 #endif
 
 #ifdef WITH_PYTHON_PATH


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-09-09 21:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-09 21:52 [binutils-gdb] Fix leak detected in python.c initialization code Philippe Waroquiers

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).