Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Matthieu Longo <matthieu.longo@arm.com>
To: Tom Tromey <tom@tromey.com>, Andrew Burgess <aburgess@redhat.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH v1] gdb/python: fix memory leak in gdb_py_tp_name
Date: Mon, 1 Jun 2026 10:57:17 +0100	[thread overview]
Message-ID: <65c7d5c1-37ba-4e49-8d2a-800421075fb2@arm.com> (raw)
In-Reply-To: <87cxye5pn0.fsf@tromey.com>

On 29/05/2026 14:09, Tom Tromey wrote:
>>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
> 
> Andrew> isn't throwing an error here going to be problematic as gdb_py_tp_name
> Andrew> is called from Python callbacks?  Unless these are catching and handling
> Andrew> exceptions then the error is going to end up trying to pass through
> Andrew> Python's C code.
> 
> Yeah.
> 
> I guess maybe it isn't explicitly stated anywhere (not sure) but the
> rule in gdb's Python layer is that gdb exceptions can't propagate into
> Python itself.  So if a function is called from Python (say like a
> __repr__ implementation), then that function has to be sure that it does
> not throw.  And since most things in gdb can throw, this is why at
> present we wrap calls into gdb's core in try/catch.
> 
> Andrew> So maybe just returning something like "<type name unavailable>" would
> Andrew> be better?  This might be better even when the safety work _is_ merged;
> Andrew> if gdb_py_tp_name is called as part of logging then we'd probably rather
> Andrew> log "<type name unavailable>" than have GDB throw an exception?
> 
> Yeah, I think this is what should be done, since IIUC the cases where
> this can set the Python exception are pathological in nature.
> 
> Or just propagating the Python exception.  Though this would mean using
> a return type other than std::string.
> 
> Tom

Adopting the approach proposed by Andrew.

Matthieu

  /* Return the type's fully qualified name from a PyTypeObject.  */
-const char *
+std::string
  gdb_py_tp_name (PyTypeObject *py_type) noexcept
  {
+  static const std::string NO_TYPE_NAME = "<type name unavailable>";
+
+  auto pyobj_to_str = [&](PyObject *name) -> std::string
+  {
+    const char *s = PyUnicode_AsUTF8AndSize (name, nullptr);
+    if (s == nullptr)
+      return NO_TYPE_NAME;
+    return 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 PyUnicode_AsUTF8AndSize (fully_qualified_name, nullptr);
+    return NO_TYPE_NAME;
+  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 +53,10 @@ 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 PyUnicode_AsUTF8AndSize (qualname, nullptr);
+    return NO_TYPE_NAME;
+  return pyobj_to_str (qualname.get ());

  # else
    /* In the absence of PyType_GetQualName(), fallback on using PyHeapTypeObject
@@ -58,15 +66,14 @@ 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 PyUnicode_AsUTF8AndSize (ht->ht_qualname, nullptr);
+    return NO_TYPE_NAME;
+  return pyobj_to_str (ht->ht_qualname);
  # endif
  #endif
  }

  reply	other threads:[~2026-06-01  9:59 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
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 [this message]
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=65c7d5c1-37ba-4e49-8d2a-800421075fb2@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