]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commit
gdb/python: fix memory leak in gdb_py_tp_name
authorMatthieu Longo <matthieu.longo@arm.com>
Tue, 26 May 2026 10:46:29 +0000 (11:46 +0100)
committerMatthieu Longo <matthieu.longo@arm.com>
Thu, 4 Jun 2026 14:09:54 +0000 (15:09 +0100)
commitdafd73bcda60e9a54bdd43c79337dce452b1d8af
tree4053039ae848a1997a543474a5dc3d78babce65e
parentfc7126f8630423ce0224f83e2688fd14fc01d2b7
gdb/python: fix memory leak in gdb_py_tp_name

The current implementation of gdb_py_tp_name() leaks a reference to the
object returned by PyType_GetFullyQualifiedName() for Python >= 3.13, and
PyType_GetQualName() for Python >= 3.11.

Managing the strong reference on the returned PyObject* with a gdbpy_ref<>
fixes the reference count, but also causes the temporary object to be
deallocated on function exit. As a consequence, the 'const char *' returned
by PyUnicode_AsUTF8AndSize() becomes dangling and can no longer safely be
returned.

The proposed approach consists in changing gdb_py_tp_name() to return a
std::string, and forcing a copy of the 'const char *' value, statically
or dynamically allocated, stored in a temporary or non-temporary PyObject,
depending on the version of Python it was compiled with.
An unfortunate side effect of this fix is that every call sites where the
tp_name is printed, must now use `.c_str()' because PyErr_Format()
and its siblings cannot handle std::string.

Approved-By: Andrew Burgess <aburgess@redhat.com>
19 files changed:
gdb/python/py-arch.c
gdb/python/py-block.c
gdb/python/py-breakpoint.c
gdb/python/py-connection.c
gdb/python/py-corefile.c
gdb/python/py-disasm.c
gdb/python/py-frame.c
gdb/python/py-infthread.c
gdb/python/py-mi.c
gdb/python/py-micmd.c
gdb/python/py-obj-type.c
gdb/python/py-obj-type.h
gdb/python/py-style.c
gdb/python/py-symbol.c
gdb/python/py-type.c
gdb/python/py-unwind.c
gdb/python/py-utils.c
gdb/python/python-internal.h
gdb/python/python.c