Skip to content

Commit e8e7d10

Browse files
committed
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.
1 parent 7db85ad commit e8e7d10

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

‎gdb/ChangeLog‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2017-11-26 Simon Marchi <simon.marchi@polymtl.ca>
2+
3+
* python/python.c (do_start_initialization): Change progname
4+
type to gdb::unique_xmalloc_ptr. Release the pointer when using
5+
Python 2.
6+
17
2017-11-26 Tom Tromey <tom@tromey.com>
28

39
* common/format.h: Add include guards.

‎gdb/python/python.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,6 @@ finalize_python (void *ignore)
16581658
static bool
16591659
do_start_initialization ()
16601660
{
1661-
char *progname;
16621661
#ifdef IS_PY3K
16631662
int i;
16641663
size_t progsize, count;
@@ -1672,19 +1671,20 @@ do_start_initialization ()
16721671
/foo/bin/python
16731672
/foo/lib/pythonX.Y/...
16741673
This must be done before calling Py_Initialize. */
1675-
progname = concat (ldirname (python_libdir).c_str (), SLASH_STRING, "bin",
1676-
SLASH_STRING, "python", (char *) NULL);
1674+
gdb::unique_xmalloc_ptr<char> progname
1675+
(concat (ldirname (python_libdir).c_str (), SLASH_STRING, "bin",
1676+
SLASH_STRING, "python", (char *) NULL));
16771677
#ifdef IS_PY3K
16781678
std::string oldloc = setlocale (LC_ALL, NULL);
16791679
setlocale (LC_ALL, "");
1680-
progsize = strlen (progname);
1680+
progsize = strlen (progname.get ());
16811681
progname_copy = (wchar_t *) PyMem_Malloc ((progsize + 1) * sizeof (wchar_t));
16821682
if (!progname_copy)
16831683
{
16841684
fprintf (stderr, "out of memory\n");
16851685
return false;
16861686
}
1687-
count = mbstowcs (progname_copy, progname, progsize + 1);
1687+
count = mbstowcs (progname_copy, progname.get (), progsize + 1);
16881688
if (count == (size_t) -1)
16891689
{
16901690
fprintf (stderr, "Could not convert python path to string\n");
@@ -1697,7 +1697,7 @@ do_start_initialization ()
16971697
it is not freed after this call. */
16981698
Py_SetProgramName (progname_copy);
16991699
#else
1700-
Py_SetProgramName (progname);
1700+
Py_SetProgramName (progname.release ());
17011701
#endif
17021702
#endif
17031703

0 commit comments

Comments
 (0)