From: Matthieu Longo <matthieu.longo@arm.com>
To: Tom Tromey <tom@tromey.com>
Cc: gdb-patches@sourceware.org, Andrew Burgess <aburgess@redhat.com>
Subject: Re: [PATCH v1] gdb/python: fix memory leak in gdb_py_tp_name
Date: Thu, 28 May 2026 13:46:43 +0100 [thread overview]
Message-ID: <11b35b46-7e18-471a-94e2-91fef5b287d2@arm.com> (raw)
In-Reply-To: <87fr3e6sdp.fsf@tromey.com>
On 26/05/2026 17:35, Tom Tromey wrote:
>>>>>> "Matthieu" == Matthieu Longo <matthieu.longo@arm.com> writes:
>
> Matthieu> The proposed approach consists in changing gdb_py_tp_name() to return a
> Matthieu> std::string
>
> That seems fine.
>
> Matthieu> A unfortunate side effect of this fix is that every call sites where the
> Matthieu> tp_name is printed, must now use `.c_str()' because PyErr_Format()
> Matthieu> and its siblings cannot handle std::string.
>
> Yeah, that's unfortunate but normal.
>
> Matthieu> /* Return the type's fully qualified name from a PyTypeObject. */
> Matthieu> -const char *
> Matthieu> +std::string
> Matthieu> gdb_py_tp_name (PyTypeObject *py_type) noexcept
> Matthieu> {
> Matthieu> #if PY_VERSION_HEX >= 0x030d0000
> Matthieu> /* Note: PyType_GetFullyQualifiedName() was added in version 3.13, and is
> Matthieu> part of the stable ABI since version 3.13. */
> Matthieu> - PyObject *fully_qualified_name = PyType_GetFullyQualifiedName (py_type);
> Matthieu> + gdbpy_ref<> fully_qualified_name (PyType_GetFullyQualifiedName (py_type));
> Matthieu> if (fully_qualified_name == nullptr)
> Matthieu> return nullptr;
>
> First, returning nullptr here seems really weird. I think this should
> cause a crash? In C++23 this constructor is even deleted.
>
> Second, if PyType_GetFullyQualifiedName fails, this will violate the
> invariants gdb normally uses for error handling.
>
> Matthieu> - return PyUnicode_AsUTF8AndSize (fully_qualified_name, nullptr);
> Matthieu> + return PyUnicode_AsUTF8AndSize (fully_qualified_name.get (), nullptr);
>
> I think this can also fail.
>
> Matthieu> + gdbpy_ref<> qualname (PyType_GetQualName (py_type));
> Matthieu> if (qualname == nullptr)
> Matthieu> return nullptr;
>
> Matthieu> - return PyUnicode_AsUTF8AndSize (qualname, nullptr);
> Matthieu> + return PyUnicode_AsUTF8AndSize (qualname.get (), nullptr);
>
> Similar stuff in these spots.
>
> Tom
Sorry, I completely missed those issues while I was changing the code in others places.
I should definitely have spotted them if it was not my thoughtlessness in those last days.
Here below is the diff of gdb_py_tp_name() with fixes for all the issues you previously mentioned.
Thanks for your patience with me, Tom.
Matthieu
--- a/gdb/python/py-obj-type.c
+++ b/gdb/python/py-obj-type.c
@@ -21,17 +21,23 @@
#include "py-obj-type.h"
/* Return the type's fully qualified name from a PyTypeObject. */
-const char *
+std::string
gdb_py_tp_name (PyTypeObject *py_type) noexcept
{
+ auto pyobj_to_str = [](PyObject *name) -> std::string
+ {
+ const char *s = PyUnicode_AsUTF8AndSize (name, nullptr);
+ return (s == nullptr) ? "" : s;
+ };
+
#if PY_VERSION_HEX >= 0x030d0000
/* Note: PyType_GetFullyQualifiedName() was added in version 3.13, and is
part of the stable ABI since version 3.13. */
- PyObject *fully_qualified_name = PyType_GetFullyQualifiedName (py_type);
+ gdbpy_ref<> fully_qualified_name (PyType_GetFullyQualifiedName (py_type));
if (fully_qualified_name == nullptr)
- return nullptr;
+ return {};
- return PyUnicode_AsUTF8AndSize (fully_qualified_name, nullptr);
+ return pyobj_to_str (fully_qualified_name.get ());
#else /* PY_VERSION_HEX < 0x030d0000 && ! defined (Py_LIMITED_API) */
/* For non-heap types, the fully qualified name corresponds to tp_name. */
@@ -44,11 +50,11 @@ gdb_py_tp_name (PyTypeObject *py_type) noexcept
# if PY_VERSION_HEX >= 0x030b0000
/* Note: PyType_GetQualName() was added in version 3.11. */
- PyObject *qualname = PyType_GetQualName (py_type);
+ gdbpy_ref<> qualname (PyType_GetQualName (py_type));
if (qualname == nullptr)
- return nullptr;
+ return {};
- return PyUnicode_AsUTF8AndSize (qualname, nullptr);
+ return pyobj_to_str (qualname.get ());
# else
/* In the absence of PyType_GetQualName(), fallback on using PyHeapTypeObject
@@ -58,15 +64,15 @@ gdb_py_tp_name (PyTypeObject *py_type) noexcept
when the minimum supported Python version is increased above 3.10. */
PyHeapTypeObject *ht = (PyHeapTypeObject *) py_type;
if (ht->ht_qualname == nullptr)
- return nullptr;
+ return {};
- return PyUnicode_AsUTF8AndSize (ht->ht_qualname, nullptr);
+ return pyobj_to_str (ht->ht_qualname);
# endif
#endif
}
next prev parent reply other threads:[~2026-05-28 12:49 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-26 16:04 Matthieu Longo
2026-05-26 16:35 ` Tom Tromey
2026-05-28 12:46 ` Matthieu Longo [this message]
2026-05-28 16:43 ` Tom Tromey
2026-05-28 17:08 ` Matthieu Longo
2026-05-28 19:00 ` Tom Tromey
2026-05-29 10:08 ` Matthieu Longo
2026-05-29 10:52 ` Andrew Burgess
2026-05-29 13:09 ` Tom Tromey
2026-06-01 9:57 ` Matthieu Longo
2026-06-02 9:36 ` Andrew Burgess
2026-06-02 14:22 ` Tom Tromey
2026-06-03 16:30 ` Matthieu Longo
2026-06-04 9:30 ` Andrew Burgess
2026-06-05 20:49 ` Tom Tromey
2026-06-08 10:31 ` Matthieu Longo
2026-06-08 18:51 ` Andrew Burgess
2026-06-05 10:06 ` Tom de Vries
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=11b35b46-7e18-471a-94e2-91fef5b287d2@arm.com \
--to=matthieu.longo@arm.com \
--cc=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox