python: Fix memleak in do_start_initialization
Commit Message
While playing with valgrind, I noticed that the progname variable in
do_start_initialization is not being freed (concat returns a malloc'ed
string). Use a unique_xmalloc_ptr to manage it.
gdb/ChangeLog:
* python/python.c (do_start_initialization): Change progname
type to gdb::unique_xmalloc_ptr.
---
gdb/python/python.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
Comments
>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:
Simon> While playing with valgrind, I noticed that the progname variable in
Simon> do_start_initialization is not being freed (concat returns a malloc'ed
Simon> string). Use a unique_xmalloc_ptr to manage it.
I don't think this is quite correct.
On the Python 2 branch, the code does this:
Py_SetProgramName (progname);
The docs for Py_SetProgramName say:
The argument should point to a
zero-terminated character string in static storage whose contents
will not change for the duration of the program's execution.
I think a comment referring to the Python docs would be better.
And, freeing progname on the Python 3 branch is ok.
Maybe the Python 2 branch could pass "progname.release ()".
Tom
@@ -1658,7 +1658,7 @@ finalize_python (void *ignore)
static bool
do_start_initialization ()
{
- char *progname;
+ gdb::unique_xmalloc_ptr<char> progname;
#ifdef IS_PY3K
int i;
size_t progsize, count;
@@ -1672,19 +1672,19 @@ 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);
+ progname.reset (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");