From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 8XapF/ZvGWqgwCYAWB0awg (envelope-from ) for ; Fri, 29 May 2026 06:52:38 -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=NVemoGYL; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 5CCC31E0A3; Fri, 29 May 2026 06:52:38 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.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,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.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 D7B761E062 for ; Fri, 29 May 2026 06:52:37 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 98F184BA23CB for ; Fri, 29 May 2026 10:52:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 98F184BA23CB 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=NVemoGYL 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 A4B8F4BA2E2A for ; Fri, 29 May 2026 10:52:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A4B8F4BA2E2A 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 A4B8F4BA2E2A 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=1780051931; cv=none; b=YKixWgpfIzJ85QeaRsdGF1Rx7e84aZeJeCq+vZGZHEIZtFaP1to+M/WL2z7FSNT/g/hdQNTQZfqB3Sa1+gKUNiF3IdBhz+W+T+66SfkWXkSFLRqvFRU4FuIOOA0dd63+EAlw8fJn/TlDxc5PZoDO+NrblI1KO0Eh176+rT0k1iA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1780051931; c=relaxed/simple; bh=uUlTzc/44bbxyBwcb3+VE6euIzrv9NwH3qWecy6sJTg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=CdL64r4j4V6iVI6sAva7OWC27FGLdedzfDDoAdd3eS0iN0ZFhlu59FTuQQgsDpWCgNpYUYvBYTUolZHkE2Dp+L2zDibmeYmk+ZHbq5Zw9emavVLG80bsk7tgXGbIxdmdC//hiJ7cdJEkYxQSrH1mC5k05k6afAlxF+RlpssIPc8= 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=NVemoGYL DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A4B8F4BA2E2A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780051931; 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=CV8g/7wtoOmn/yWlsT8GEqO41gQ9ab67/rmzeno6zaQ=; b=NVemoGYLXb4iFgJVj3dVKzraGTrQFP06qtFlSzD0+9qx4mqtLeaWIQwjek5VYCxOUqjvmz n/Ixp38DIpT3GW+NuOU6pFpykmYOaRYIqtLK7HFSTjHybcYjoXj0fVjJCnQtv6Jd/8vQWM NTN+Zae4y+2I9PJcMe76dSH/TTrAFT8= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-416-fdfHCRU-Myas452niB-UcA-1; Fri, 29 May 2026 06:52:10 -0400 X-MC-Unique: fdfHCRU-Myas452niB-UcA-1 X-Mimecast-MFC-AGG-ID: fdfHCRU-Myas452niB-UcA_1780051929 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-44dad1b938fso7948825f8f.1 for ; Fri, 29 May 2026 03:52:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780051929; x=1780656729; 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=CV8g/7wtoOmn/yWlsT8GEqO41gQ9ab67/rmzeno6zaQ=; b=oxVQYm86qjmA1s8vpYsF+2rZyl4GrxvPGF6x1c6DTkKbYtVIknZl9TGhfuqdCdZgVf ZyXhKft+6uCHFIdwlEzFmTH+IJHuy1b03WIwKL9bJkqD3w7GGue6/bT8qacNJxTUdr8i EIr+TRcUJ9nlqCzUkgwrOrfVQVTmYnuVgXUUfuPhpZQgfs/GhAyFzdZiLLqbcRNT20qY 5gbyPAqjewCXXa9Ts0bw52Bsihb6QG7VlfF/EXr71K0y+7jeEjhEbjHRnuzrDec4zIUz EJw20AUVOmn/VikYb4SNL3ERNpfrFgMMTZoQ5RKAw6b2JcgXsmu1zg++nsLdXDpuFS3x l7rA== X-Gm-Message-State: AOJu0Yxosv1utBs2aTnuklu7HiqYO9G5RSwqOLmRk+vQpNPEHKyh2zfE B2M0amsrrgZPvBRHN6Ss5WzlGxdhzFAMQbIrbV3mjtiEwQmOMpHbqwnc1Wc50BWjGmWQTNElxF2 B5N5C7RvLW+XY3vh9yQyp7WrOGLIoQ6GTDvyF+xROP9sVjNIz2CzFs7Z39l5PQlc= X-Gm-Gg: Acq92OHvtgo1EAjQeay+KBHOxq8VHOQYD89Arh9n6oKfAvyg518Mpt6OWuOGeH+cXxz GlWLZ3eYHkTZKYCkNDsovj/cru3n6JCnxG77ZVLagPqp0+HnnlHL9JTRS1cAwPoIg6kxfB8lrQ3 kHxCXrQklbxZ3hwd5AtfKjdRD0VbfsXQHvh2m2idEW2WUzb1W2hQiS4dId8IlpkQHIYjV/feBFX WxHt/ZbXZgcg1Pty89S7FCq3ghklN4Wd2py1uhTBPyiOFbhil2vEEtn+zdvcQDFF2NpM9aITItk OsLwVVaOELlv+1Zrz1+NzgeRRaAt2OlH/qSgjG9QyoDb//D3Uan88nU9IPH7i30MjQZMqfDq5uP FH2VI9W6SYJ1iC1BrMizDZisvhQ== X-Received: by 2002:a05:600c:3488:b0:490:9bc2:bf8b with SMTP id 5b1f17b1804b1-4909c0799cfmr50211115e9.5.1780051928732; Fri, 29 May 2026 03:52:08 -0700 (PDT) X-Received: by 2002:a05:600c:3488:b0:490:9bc2:bf8b with SMTP id 5b1f17b1804b1-4909c0799cfmr50210495e9.5.1780051928274; Fri, 29 May 2026 03:52:08 -0700 (PDT) Received: from localhost ([213.31.44.43]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4909c13d018sm15219635e9.8.2026.05.29.03.52.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2026 03:52:07 -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> Date: Fri, 29 May 2026 11:52:06 +0100 Message-ID: <87zf1ixzc9.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 2Tv0yNzlgjIVLCIsQqY5GMfFop5XxujYKVLmI4ArbUU_1780051929 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 28/05/2026 20:00, Tom Tromey wrote: >>>>>>> "Matthieu" == Matthieu Longo writes: >> >> Matthieu> Indeed that can happen, for instance, when this function is called >> Matthieu> inside a tp_clear() in the middle of a PyObject destruction. >> >> Thanks. >> >> Matthieu> Something like below would at least avoid the general abort, but it >> Matthieu> would cause logging of errors in the middle of the destruction. How >> Matthieu> useful would it be ? >> >> Matthieu> gdbpy_ref<> fully_qualified_name (PyType_GetFullyQualifiedName (py_type)); >> Matthieu> if (fully_qualified_name == nullptr) >> Matthieu> { >> Matthieu> PyErr_Print(); >> Matthieu> PyErr_Clear(); >> Matthieu> return {}; >> Matthieu> } >> >> Matthieu> At this point, I don't think that it is super important not to crash >> Matthieu> because there is a serious logic error somewhere if we are unable to >> Matthieu> get this name. Consequently, an assert might be more appropriate. >> >> If we think it could conceivably happen then it should probably just >> call gdbpy_print_stack (which is basically the above but with some user >> controls) and then return some error string. >> >> Tom > > What about the below ? > > Matthieu > > diff --git a/gdb/python/py-obj-type.c b/gdb/python/py-obj-type.c > index 45f7d8a3d74..514dbf5c179 100644 > --- a/gdb/python/py-obj-type.c > +++ b/gdb/python/py-obj-type.c > @@ -27,7 +27,11 @@ 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 (s != nullptr) > + return s; > + > + gdbpy_print_stack (); > + error (_("Could not convert PyObject to UTF-8 string.")); I'm a bit late to this review chain, but... isn't throwing an error here going to be problematic as gdb_py_tp_name is called from Python callbacks? Unless these are catching and handling exceptions then the error is going to end up trying to pass through Python's C code. Of course, with Tom's upcoming safety work that would be fine as all of these exceptions would be caught and handled correctly. But for now you likely don't want to have to add try/catch everywhere gdb_py_tp_name is used. So maybe just returning something like "" would be better? This might be better even when the safety work _is_ merged; if gdb_py_tp_name is called as part of logging then we'd probably rather log "" than have GDB throw an exception? Thanks, Andrew > }; > > #if PY_VERSION_HEX >= 0x030d0000 > @@ -35,8 +39,10 @@ gdb_py_tp_name (PyTypeObject *py_type) noexcept > part of the stable ABI since version 3.13. */ > gdbpy_ref<> fully_qualified_name (PyType_GetFullyQualifiedName (py_type)); > if (fully_qualified_name == nullptr) > - return {}; > - > + { > + gdbpy_print_stack (); > + error (_("Could not get fully qualified name.")); > + } > return pyobj_to_str (fully_qualified_name.get ()); > > #else /* PY_VERSION_HEX < 0x030d0000 && ! defined (Py_LIMITED_API) */ > @@ -52,8 +58,10 @@ gdb_py_tp_name (PyTypeObject *py_type) noexcept > /* Note: PyType_GetQualName() was added in version 3.11. */ > gdbpy_ref<> qualname (PyType_GetQualName (py_type)); > if (qualname == nullptr) > - return {}; > - > + { > + gdbpy_print_stack (); > + error (_("Could not get qualified name.")); > + } > return pyobj_to_str (qualname.get ()); > > # else > @@ -64,8 +72,10 @@ 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 {}; > - > + { > + gdbpy_print_stack (); > + error (_("Could not get qualified name.")); > + } > return pyobj_to_str (ht->ht_qualname); > # endif > #endif