From patchwork Thu May 21 23:27:52 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 135428 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 64CED48F60C7 for ; Thu, 21 May 2026 23:28:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 64CED48F60C7 Authentication-Results: sourceware.org; dkim=fail reason="signature verification failed" (768-bit key, unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=TNXi3Bin X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from omta038.useast.a.cloudfilter.net (omta038.useast.a.cloudfilter.net [44.202.169.37]) by sourceware.org (Postfix) with ESMTPS id 51C4948F90F1 for ; Thu, 21 May 2026 23:27:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 51C4948F90F1 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 51C4948F90F1 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=44.202.169.37 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779406079; cv=none; b=VbZfMrA2LBKoydH+ux5ihWwErRsRUtu+ZnBoyHEY78faQAQhQbjKgV4KhxGRcC43XaNQu+wA3f5pVMspua+5llH9Qg4wI85Re8FLHuIVtOoWwAcjwOBNm/V2ZFP/6VM8fJPgr9RASeXqGpr841mjoMWfcBcMnrHVrzzpfJm2G+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779406079; c=relaxed/simple; bh=NHHIP/FvjVSN0QB/9KCb3hB0upclEs0k4wCaNITPNTY=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=jPkn8/xqMgi4bHQEF18T2BD4y0t4jXHHQhOtT7itWRaK2eRbZpodV9SuhsqD3yb6p1pDriZ2N0B18OXRqmTKIJZPR6fe0CJtBQDKDb53UbdjR2mjSORVB9RcOysB0/RKgg+yhWOnkiu00CIBqam6DJHF++UJ+umiLkYB8EV3vEk= ARC-Authentication-Results: i=1; sourceware.org; dkim=policy (768-bit key, unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=TNXi3Bin reason="signing key too small" DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 51C4948F90F1 Received: from eig-obgw-6007b.ext.cloudfilter.net ([10.0.30.166]) by cmsmtp with ESMTPS id QCN1wwpHYKSRRQCo2w0zp9; Thu, 21 May 2026 23:27:59 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id QCo1w5JdBcYN5QCo2wBrXG; Thu, 21 May 2026 23:27:58 +0000 X-Authority-Analysis: v=2.4 cv=QN5oRhLL c=1 sm=1 tr=0 ts=6a0f94fe a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=IkcTkHD0fZMA:10 a=NGcC8JguVDcA:10 a=ItBw4LHWJt0A:10 a=CsyaYzD9I3rCOnvogYcA:9 a=QEXdDO2ut3YA:10 a=DCx65vhANUyCzuf5D8fC:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Cc:To:In-Reply-To:References:Message-Id: Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From:Sender: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=ZQefP/OTmt8OYcAH6PtM+vJ1XP4jjnHxq0uOKebrMKw=; b=TNXi3BinNFjgcd+NEpZR5Mehh7 ZRFEsC/duzupBLTbEJVqB4VlLJCnk/GNsHRIeiibLcyEv2/Y9YZZxU81UlYC3HLGxw+ORlTvafzRi thmHY4rgwu9Yr4U7DuMSTIijQ; Received: from 75-166-225-82.hlrn.qwest.net ([75.166.225.82]:37536 helo=[192.168.0.17]) by box5379.bluehost.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.2) (envelope-from ) id 1wQCo1-00000002l41-3H7E; Thu, 21 May 2026 17:27:57 -0600 From: Tom Tromey Date: Thu, 21 May 2026 17:27:52 -0600 Subject: [PATCH v3 1/4] Add gdbpy_borrowed_ref MIME-Version: 1.0 Message-Id: <20260521-python-safety-initial-v3-1-d0679c36e499@tromey.com> References: <20260521-python-safety-initial-v3-0-d0679c36e499@tromey.com> In-Reply-To: <20260521-python-safety-initial-v3-0-d0679c36e499@tromey.com> To: gdb-patches@sourceware.org Cc: Tom Tromey X-Mailer: b4 0.14.3 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 75.166.225.82 X-Source-L: No X-Exim-ID: 1wQCo1-00000002l41-3H7E X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-225-82.hlrn.qwest.net ([192.168.0.17]) [75.166.225.82]:37536 X-Source-Auth: tom+tromey.com X-Email-Count: 4 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfF3g1ZgO/LRNUvISYTh+t1HmBi5NoaB+CL254I4qTVY2b/PSk2irwzFf8zJWh8noaEFqsMD4J8hWDG3M+ijQyZMGt5tukg87A2QxXl2yO1xgoT7x2RVN e7sSBlMjO22lpaXlvwyVF0BNWJNP4DgOOxABfrs/Zlv1GRPpmnF23gAAQU395BrLmTMPHBGoWx9ZhYYBRN8vipuwM7U0lglUlNU= X-Spam-Status: No, score=-3012.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, SPF_HELO_NONE, TXREP, T_SPF_PERMERROR shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org 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~patchwork=sourceware.org@sourceware.org This adds new gdbpy_opt_borrowed_ref and gdbpy_borrowed_ref classes. These classes are primarily for code "documentation" purposes -- it makes it clear to the reader that a given reference is borrowed. However, they also add a tiny bit of safety, in that conversion to gdbpy_ref<> will either be rejected (by the "opt" class) or acquire a new reference. --- gdb/python/py-ref.h | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h index 3d2906fd7f5..3d0373b7001 100644 --- a/gdb/python/py-ref.h +++ b/gdb/python/py-ref.h @@ -21,6 +21,7 @@ #define GDB_PYTHON_PY_REF_H #include "gdbsupport/gdb_ref_ptr.h" +#include "gdbsupport/traits.h" #include "python-traits.h" /* A policy class for gdb::ref_ptr for Python reference counting. */ @@ -42,6 +43,91 @@ struct gdbpy_ref_policy template using gdbpy_ref = gdb::ref_ptr; +/* A class representing an optional borrowed reference. It is + "optional" because NULL is a valid value. + + This is a simple wrapper for a pointer to PyObject or some subclass + of it. Aside from documenting what the code does, the main + advantage of using this is that conversion to a gdbpy_ref is + prevented. + + An optional borrowed reference is only used in situations where + Python says NULL is valid. For example, it is used as the type of + the "keywords" argument to a varargs method. Most code should + prefer an ordinary gdbpy_borrowed_ref, see below. */ +template +class gdbpy_opt_borrowed_ref +{ +public: + + gdbpy_opt_borrowed_ref (T *obj) + : m_obj (obj) + { + } + + template + gdbpy_opt_borrowed_ref (const gdbpy_ref &ref) + : m_obj (ref.get ()) + { + } + + operator T * () const + { + return m_obj; + } + + operator gdbpy_ref () = delete; + +protected: + + T *m_obj; +}; + +/* A borrowed reference that is guaranteed not to be NULL. + + Like gdbpy_opt_borrowed_ref, this mostly serves a documentary + purpose. However, it also allows a checked cast to any subclass of + T, and conversion to a gdbpy_ref will automatically acquire a + new reference -- a safety improvement over plain PyObject * or the + like. */ +template +class gdbpy_borrowed_ref : public gdbpy_opt_borrowed_ref +{ +public: + + gdbpy_borrowed_ref (T *obj) + : gdbpy_opt_borrowed_ref (obj) + { + gdb_assert (this->m_obj != nullptr); + } + + template + gdbpy_borrowed_ref (const gdbpy_ref &ref) + : gdbpy_opt_borrowed_ref (ref) + { + gdb_assert (this->m_obj != nullptr); + } + + gdbpy_borrowed_ref (std::nullptr_t) = delete; + + /* Allow a (checked) conversion to any subclass of T. */ + template>> + operator U * () const + { + gdb_assert (PyObject_TypeCheck (this->m_obj, + U::corresponding_object_type)); + return static_cast (this->m_obj); + } + + /* When converting a borrowed reference to a gdbpy_ref<>, a new + reference is acquired. */ + operator gdbpy_ref () const + { + return gdbpy_ref::new_reference (this->m_obj); + } +}; + /* A wrapper class for Python extension objects that have a __dict__ attribute. Any Python C object extension needing __dict__ should inherit from this