diff options
author | Simon Marchi <simon.marchi@polymtl.ca> | 2017-11-26 19:32:47 -0500 |
---|---|---|
committer | Simon Marchi <simon.marchi@polymtl.ca> | 2017-11-26 19:32:47 -0500 |
commit | e8e7d10c39955e7ff99ff42f6f16d6befe2fa12e (patch) | |
tree | eaf62d9bf2d3b4d1171eaf7dafea96ac454c0e72 /gdb | |
parent | 7db85adb3b988560d9d4706d826e193e83ae7cd9 (diff) | |
download | gdb-e8e7d10c39955e7ff99ff42f6f16d6befe2fa12e.zip gdb-e8e7d10c39955e7ff99ff42f6f16d6befe2fa12e.tar.gz gdb-e8e7d10c39955e7ff99ff42f6f16d6befe2fa12e.tar.bz2 |
python: Fix memleak in do_start_initialization
While playing with valgrind, I noticed that with Python 3, the progname
variable in do_start_initialization is not being freed (concat returns a
malloc'ed string). This patch uses unique_xmalloc_ptr to manage it.
With Python 2, we pass progname it directly to Py_SetProgramName, so it
should not be freed. We therefore release it before passing it.
gdb/ChangeLog:
* python/python.c (do_start_initialization): Change progname
type to gdb::unique_xmalloc_ptr. Release the pointer when using
Python 2.
Diffstat (limited to 'gdb')
-rw-r--r-- | gdb/ChangeLog | 6 | ||||
-rw-r--r-- | gdb/python/python.c | 12 |
2 files changed, 12 insertions, 6 deletions
diff --git a/gdb/ChangeLog b/gdb/ChangeLog index a901409..b3032ed 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,9 @@ +2017-11-26 Simon Marchi <simon.marchi@polymtl.ca> + + * python/python.c (do_start_initialization): Change progname + type to gdb::unique_xmalloc_ptr. Release the pointer when using + Python 2. + 2017-11-26 Tom Tromey <tom@tromey.com> * common/format.h: Add include guards. diff --git a/gdb/python/python.c b/gdb/python/python.c index 5f15261..9ed9b6b 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1658,7 +1658,6 @@ finalize_python (void *ignore) static bool do_start_initialization () { - char *progname; #ifdef IS_PY3K int i; size_t progsize, count; @@ -1672,19 +1671,20 @@ 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); + gdb::unique_xmalloc_ptr<char> progname + (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"); @@ -1697,7 +1697,7 @@ do_start_initialization () it is not freed after this call. */ Py_SetProgramName (progname_copy); #else - Py_SetProgramName (progname); + Py_SetProgramName (progname.release ()); #endif #endif |