From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id oS69CBVGIWpNCjQAWB0awg (envelope-from ) for ; Thu, 04 Jun 2026 05:32:05 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=HSQyFcZB; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 1298A1E062; Thu, 04 Jun 2026 05:32:05 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 56AB41E062 for ; Thu, 04 Jun 2026 05:32:04 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4450E4BA2E2A for ; Thu, 4 Jun 2026 09:32:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4450E4BA2E2A Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=HSQyFcZB Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 2F70A4BAE7D8 for ; Thu, 4 Jun 2026 09:30:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F70A4BAE7D8 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2F70A4BAE7D8 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1780565449; cv=none; b=aiVmtNKdfjwzXTbAF1z91OErarUegLcJmFvkamqa6sIq1qtoJrkwisQHsyvX2cVw40uYY+PJXwnMv6/Ur60yKKmxlk+bxCTqArXRLqwoeUJdsLMhKtM2xey+LCgBf0wvcoWX0laPZ0F5+sEXUNRtT1wAvC18IafcOir+4UoBc/U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1780565449; c=relaxed/simple; bh=sbTy2vMXyPDeDZZ3hzMuTK7DGVHqxUkdHELbRu2OhmI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=RfCm7zl9r79w4eV9AERe1piCeYcEQa/1eXW8z13+P2/vO+hOtoyZas4Hih1BSXFFtstc9BBglOqDjciG7QNgj4MgDH4JWqkYQScFrVcya1ajojKglpyqagiFQnQCS1ICT/uZqM7+5Xoo2QhPKZGdwR1e5Q5wUJsP8Y7sPwzQEH4= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=HSQyFcZB DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2F70A4BAE7D8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780565448; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=KD4bCDXpYAhqrjQT7HCSr+fCVOgZ861EJvMWJ6Ruv5A=; b=HSQyFcZB4ACDitqOvpIlsCv06r0y8ZarGnoDdF2u3Y5UqduTqLNXnxeeNyuyCAElUSVPMi 2G5ALaZe/wWmICPW8/PB+whZEPk55MH0LELwCVfoUJB/URlyGcEDsXlnpd/hF3dT83TLie E06kvp7mcdQ7kdkQ7d893a0SiH6Cr9A= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-678-7EZxu525OSG5S6i6ZYnMFw-1; Thu, 04 Jun 2026 05:30:47 -0400 X-MC-Unique: 7EZxu525OSG5S6i6ZYnMFw-1 X-Mimecast-MFC-AGG-ID: 7EZxu525OSG5S6i6ZYnMFw_1780565446 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-45ef0af9517so388295f8f.3 for ; Thu, 04 Jun 2026 02:30:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780565446; x=1781170246; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=KD4bCDXpYAhqrjQT7HCSr+fCVOgZ861EJvMWJ6Ruv5A=; b=bXJGhAp5oOTw2QBcODfk2LXXCBEp9QkReaQWcRPsnsPkXYse6gUJAHqL+gByGbfFIp yapv22QjMFFljdF/yJZBKddVbFVQGIEcuHikrYGLdPp2W01FOb/sTHr0keTlipthOFok C5NMK2bJamkPIYNFqsOo9o+8eRxq5LAP6t9OE/tk0cGtSye1ejgyLxcDqiUOQUTFODG/ CrnfAFw7w/yMauuiXmXI+yPYbV7gpFMvp7bWShglFl7JX0K+WrU4uRgAyHMdi4T4DfvW 6A2cqySlgFZz9MksbUB9HOMt0mxUugVdwg68aRO05ZfqlMcysUiZQvrLbdfjIm05yS9H a4aQ== X-Gm-Message-State: AOJu0YwC6e3J/QVrWJveoxlMC4A2JbrIN33FCkYy2/8PqkpjAti4ePhP T5psjaxQcEW/9B3o67MPce6w+6a2MLtlueNztfTIe5nJlIG+FBYQLO/Tt/yPmeEuTDpPb0wSeJ5 zzMOCO3HG8PFj1Y+q6mjdTyFdHUV0EkmiPziSrUrvv3ZlxdRIQd1Ls5Ma4EebCaY= X-Gm-Gg: Acq92OH6sScy2SWWrKNkRxV7ZYLLMLK5CwbWN/cS3MPkEN9tUn1lCy0G40L5l3rKk/W jmP7z+m5M8UsT37NEiQh0hI5VAPHTXGxNqHPn+TTBiJJ66X0ajWnoMU3ajpuVesz+sYrbxIyXWu dg41cKCOpOeFfbzIyhp0pYFGsMzEkzBan9DdpvqBLr3w9tiQaNIOfMRYbJTycqSPWEHSqBNN1EY mH9sbgjcmojP3veE8g+4s+2ReBTfBH4mkpqEeNqqIUbtr+ytSPK2Bv0MExp8ssIyoELmGhLAHmt PUwSehKmhr3dyy8ky7GvTEqOi9m2Yl81Bv298SwWF1Hpg8MuHJ0/KTBqhIPAnWDQ9TwDyNhT3LO +vh5jxy+gdgiLxLMKrQ/tCqStEw== X-Received: by 2002:adf:f8ca:0:b0:45e:ea46:ce13 with SMTP id ffacd0b85a97d-460217a5a76mr7939702f8f.10.1780565446325; Thu, 04 Jun 2026 02:30:46 -0700 (PDT) X-Received: by 2002:adf:f8ca:0:b0:45e:ea46:ce13 with SMTP id ffacd0b85a97d-460217a5a76mr7939639f8f.10.1780565445755; Thu, 04 Jun 2026 02:30:45 -0700 (PDT) Received: from localhost ([213.31.44.97]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4601f360bd6sm16130393f8f.36.2026.06.04.02.30.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jun 2026 02:30:45 -0700 (PDT) From: Andrew Burgess To: Matthieu Longo , Tom Tromey Cc: gdb-patches@sourceware.org Subject: Re: [PATCH v1] gdb/python: fix memory leak in gdb_py_tp_name In-Reply-To: References: <20260526160459.270322-1-matthieu.longo@arm.com> <87fr3e6sdp.fsf@tromey.com> <11b35b46-7e18-471a-94e2-91fef5b287d2@arm.com> <87tsrr5vtl.fsf@tromey.com> <62841b2c-9d0e-4efc-9b26-ca9bfd8787b9@arm.com> <87ldd35php.fsf@tromey.com> <87zf1ixzc9.fsf@redhat.com> <87cxye5pn0.fsf@tromey.com> <65c7d5c1-37ba-4e49-8d2a-800421075fb2@arm.com> <87o6hts2q0.fsf@redhat.com> <87fr35hvjb.fsf@tromey.com> Date: Thu, 04 Jun 2026 10:30:43 +0100 Message-ID: <87ecimsldo.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: GcVocGo6C8lZlR9HgyRcbBeVMbvtfVXndhhGt8eh3KA_1780565446 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org Matthieu Longo writes: > On 02/06/2026 15:22, Tom Tromey wrote: >>>>>>> "Andrew" == Andrew Burgess writes: >> >>>> + const char *s = PyUnicode_AsUTF8AndSize (name, nullptr); >>>> + if (s == nullptr) >>>> + return NO_TYPE_NAME; >> >> Andrew> If PyUnicode_AsUTF8AndSize fails then a Python exception will be set. >> Andrew> As we're not going to propagate that back to the actual Python >> Andrew> interpreter, should we not clear the exception at this point? >> >> Andrew> And the same question each time NO_TYPE_NAME is injected instead of an >> Andrew> error? >> >> Also worth noting that normally gdb doesn't simply discard Python >> exceptions. (To be clear, there are a couple of spots I think, but >> maybe they are bugs.) >> >> Normally either the exception is propagated, standard Python style; or >> it is displayed using gdbpy_print_stack. This function is used because >> it respects some user settings that control what is displayed. >> >> Tom > > Addressing the previous comment of Andrew, but using gdbpy_print_stack > as recommended by Tom. LGTM. Thanks, Andrew > > Matthieu > > diff --git a/gdb/python/py-obj-type.c b/gdb/python/py-obj-type.c > index ea0b59a8447..3ed57d0cbab 100644 > --- a/gdb/python/py-obj-type.c > +++ b/gdb/python/py-obj-type.c > @@ -21,20 +21,44 @@ > #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 > { > + static const std::string NO_TYPE_NAME = ""; > + > + /* This helper should be used for cases when the called CPython function > + informs the caller that an error occurred, and a Python error was set. */ > + auto handle_err = [&]() -> std::string > + { > + gdbpy_print_stack (); > + PyErr_Clear (); > + return NO_TYPE_NAME; > + }; > + > + /* Convert a PyObject to a UTF-8 encoded string. */ > + auto pyobj_to_str = [&](PyObject *name) -> std::string > + { > + const char *s = PyUnicode_AsUTF8AndSize (name, nullptr); > + if (s == nullptr) > + return handle_err (); > + 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); > + /* Notes: > + 1. PyType_GetFullyQualifiedName() was added in version 3.13, and is > + part of the stable ABI since version 3.13. > + 2. If an error occurs when looking up the module name (for instance, > + during the destruction of the object), PyType_GetFullyQualifiedName() > + returns NULL, and a Python error is set. */ > + gdbpy_ref<> fully_qualified_name (PyType_GetFullyQualifiedName (py_type)); > if (fully_qualified_name == nullptr) > - return nullptr; > - > - return PyUnicode_AsUTF8AndSize (fully_qualified_name, nullptr); > + return handle_err (); > + 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. */ > + /* For non-heap types, the fully qualified name corresponds to tp_name, > + which can never be NULL. */ > if (! (PyType_GetFlags (py_type) & Py_TPFLAGS_HEAPTYPE)) > return py_type->tp_name; > > @@ -43,12 +67,17 @@ gdb_py_tp_name (PyTypeObject *py_type) noexcept > cases, e.g. the module name may be missing. */ > > # if PY_VERSION_HEX >= 0x030b0000 > - /* Note: PyType_GetQualName() was added in version 3.11. */ > - PyObject *qualname = PyType_GetQualName (py_type); > + /* Notes: > + 1. PyType_GetQualName() was added in version 3.11. > + 2. On one hand, PyType_GetQualName() relies internally on ht_qualname > + which is supposed to never be NULL, therefore, does not set any Python > + error. On the other hand, PyType_GetQualName() calls internally > + PyUnicode_AsUTF8AndSize(), which when erroring, sets a Python error > + and returns NULL. */ > + gdbpy_ref<> qualname (PyType_GetQualName (py_type)); > if (qualname == nullptr) > - return nullptr; > - > - return PyUnicode_AsUTF8AndSize (qualname, nullptr); > + return handle_err (); > + return pyobj_to_str (qualname.get ()); > > # else > /* In the absence of PyType_GetQualName(), fallback on using PyHeapTypeObject > @@ -58,15 +87,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 > } > > /* Return the type's fully qualified name from a PyObject. */ > -const char * > +std::string > gdbpy_py_obj_tp_name (PyObject *self) noexcept > { > /* Note: Py_TYPE () is part of the stable ABI since version 3.14. */