From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id qUpYLavMmGnGNAMAWB0awg (envelope-from ) for ; Fri, 20 Feb 2026 16:05:47 -0500 Authentication-Results: simark.ca; dkim=pass (2048-bit key; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=AVySRYvo; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id B0D6F1E09A; Fri, 20 Feb 2026 16:05:47 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, 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 0CC701E09A for ; Fri, 20 Feb 2026 16:05:46 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id AD7A74BAD15B for ; Fri, 20 Feb 2026 21:05:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD7A74BAD15B Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=AVySRYvo Received: from mail-ot1-x335.google.com (mail-ot1-x335.google.com [IPv6:2607:f8b0:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id 24F124BA23CB for ; Fri, 20 Feb 2026 21:03:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 24F124BA23CB Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 24F124BA23CB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::335 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771621433; cv=none; b=oXiAoesN1bsK6P8k59JymxyZtTXn6Lov1SAmV/2NJh2DtL4dIAeMAgskXHwxP0rcvhKlAUQlo/9P4jEf/y4lGOqBwdNmlckuKFQrDoo2jKkpuhmIRD7TG2buLlrqXisXXyWmJj+hmQvbD1/ycDYOnAG77mcsR1aGBdX5w+TD+/M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771621433; c=relaxed/simple; bh=qfUwgdEOmcq3VxDPMPJi7Ti1HEFw71fxsQsbOJb1B/c=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=LW/phMoXdFAn2P5mbE0Suz75aXpgutouRyaw8RZir4wEOGsoSbuD3qGdN1tXwqCwi9GZeKGkBtaTKaD4lzV2dXuUM+HpgUrisCnXxVD/BQl2grfF44KLcAnHXiCLxMhgZjH5zpvMGRiJ77bw2dsAY8BleDHMvUu5Txj+1nLlV30= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 24F124BA23CB Received: by mail-ot1-x335.google.com with SMTP id 46e09a7af769-7d4c85307b2so1494126a34.0 for ; Fri, 20 Feb 2026 13:03:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1771621432; x=1772226232; darn=sourceware.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=TQ8w8Ck37ciUDWblCuVWZQBKFpcq1j+luHUs5cid+Sw=; b=AVySRYvo2h0DiXUrEbbLQFhY159BjBcEJqEWFSQqYC4gj0mkbK7jURKnD7nGWpzdTF ThQD4vBVl1woUzy5H2e0lQ3wE/nwhutIMk+bALjwsWwoa7FxFb8UyFnzhhEU78JqzyVi +SjhdOtQZbz9xiQfojVe2Y2A8RbRi0RE6DN0X+jeMtD2fqgyXWAyHwiMRC6u4GZNN3/5 xmybFDWW9/CP+7E527G18yxGYhvN3vVY+yKrJF2W66oJRtdzd+vwQ/YhGFDWfc7YaLbQ 9V9+OYz2B2rsJ68wWOMu4RwaYuHX9LPEE2M1rtGTdqRq+hSl2yKGI2QAeavZJ+VLJI9t bItw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771621432; x=1772226232; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=TQ8w8Ck37ciUDWblCuVWZQBKFpcq1j+luHUs5cid+Sw=; b=m/10kWjwOSmfiYNeW0XoyDtNlxl/Eze+BkI9+N20Om7niT0rPEGfbFDrlAeDknRbCQ HL+ud+iuo5Hq6TMZykqHGSCl8jqqaH2JRC6QgM7jXLGTnHRGvi23shkHMYUfpVpNJKmu q0tGo08+Ewp3vDTeCADlSvzAyhPFq+zc0GhT7sclvIq/Ma4NiL2WQzYMYufnl3YBqTXU 766e/jwX9YlzPR16Aht4DueaRymjzdF1IIZKf0A9Wj2GlanAmFGM7i5RI4PRPLGh1r9X mQYyNEVeVnsHKciNeDPenPKP4LdlSatVXP72f5o+Y7q80wewTLOm5ks+HdtD0ptEsBO5 S9ZA== X-Gm-Message-State: AOJu0YwMSVBMEXdbQfbPtCT6CDF0IrhlHbf/snKCDKt5GfkK7lcxBd4u BCX8HNvBknsLGZG6ZwJ/b0Xu6dr4pnvoU/f7qOqD0HSZ8iFn3AFDNOnk6myqcTjauYGhKh+i73E w9eQ= X-Gm-Gg: AZuq6aKo93TQSd6JDX6PoSXdKT3iF+gEb78HfgaYaCJAUGf0irj9kvIEdpmQGf8tK+d rm4HGputUXsGZ8QRhTZrWrPZu+GzzFZJTElkCqoCiFSpZCsE/zc1CQWR8Mq7DIAsNV4Q7KsqJFl pk6yVxi2Y6pvRFJb+nXclO+FcLcQxpxIH3qYQm++0uMHop6qGO7U9i3rt/nmrbWYQ76pNMZd0X7 J0ZI3CXGSXKdsL7NxFfNbdp8dhwACem9fyiizIYr2XhwiI0wEIrM5yOXQQ1O3RmtM2fv8uD2a33 m2uo8fMRKb2ieA2mwVFws8KQ0Xt9Z8P4iBUUH/tq53+pveQgEGA6buRmyaVMc+YrGQH4g3DyP0m F0XVEL4zjWi6xLKkqB9/wowfUNuuuwJBoTo1Vm7GaVLEOzcvjfPzaLkg0q52U9xVBuJAwsbFtHW ySTs9qNirZ/U72wyP/+1HIKuz5AgBtpSwY+wrS+nGMPulb6a4CdJ5LlNhA7SHcSK43xG0= X-Received: by 2002:a05:6820:4611:b0:659:9a49:8dea with SMTP id 006d021491bc7-679c44d0f8amr547969eaf.44.1771621432265; Fri, 20 Feb 2026 13:03:52 -0800 (PST) Received: from [192.168.0.26] (97-122-122-234.hlrn.qwest.net. [97.122.122.234]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4157d2d7826sm561630fac.10.2026.02.20.13.03.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Feb 2026 13:03:51 -0800 (PST) From: Tom Tromey Date: Fri, 20 Feb 2026 14:03:50 -0700 Subject: [PATCH 04/10] Return gdbpy_ref<> from block_to_block_object MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260220-python-safety-minor-v1-4-4c4b12e445af@adacore.com> References: <20260220-python-safety-minor-v1-0-4c4b12e445af@adacore.com> In-Reply-To: <20260220-python-safety-minor-v1-0-4c4b12e445af@adacore.com> To: gdb-patches@sourceware.org Cc: Tom Tromey X-Mailer: b4 0.14.3 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 This changes block_to_block_object to return a gdbpy_ref<>, using the type system to convey that a new reference is always returned. --- gdb/python/py-block.c | 20 +++++++------------- gdb/python/py-frame.c | 3 ++- gdb/python/py-progspace.c | 2 +- gdb/python/py-symtab.c | 6 ++++-- gdb/python/py-unwind.c | 3 ++- gdb/python/python-internal.h | 4 ++-- 6 files changed, 18 insertions(+), 20 deletions(-) diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c index 718aca17829..263819e1292 100644 --- a/gdb/python/py-block.c +++ b/gdb/python/py-block.c @@ -143,7 +143,7 @@ blpy_get_superblock (PyObject *self, void *closure) super_block = block->superblock (); if (super_block) - return block_to_block_object (super_block, self_obj->objfile); + return block_to_block_object (super_block, self_obj->objfile).release (); Py_RETURN_NONE; } @@ -168,7 +168,7 @@ blpy_get_subblocks (PyObject *self, void *closure) { if (each->superblock () == block) { - gdbpy_ref<> item (block_to_block_object (each, cu->objfile ())); + gdbpy_ref<> item = block_to_block_object (each, cu->objfile ()); if (item.get () == nullptr || PyList_Append (list.get (), item.get ()) == -1) @@ -192,9 +192,7 @@ blpy_get_global_block (PyObject *self, void *closure) global_block = block->global_block (); - return block_to_block_object (global_block, - self_obj->objfile); - + return block_to_block_object (global_block, self_obj->objfile).release (); } /* Return the static block associated to this block. Return None @@ -214,7 +212,7 @@ blpy_get_static_block (PyObject *self, void *closure) static_block = block->static_block (); - return block_to_block_object (static_block, self_obj->objfile); + return block_to_block_object (static_block, self_obj->objfile).release (); } /* Implementation of gdb.Block.is_global (self) -> Boolean. @@ -333,7 +331,7 @@ blpy_dealloc (PyObject *obj) /* Create a new block object (gdb.Block) that encapsulates the struct block object from GDB. */ -PyObject * +gdbpy_ref<> block_to_block_object (const struct block *block, struct objfile *objfile) { htab_t table = blpy_objfile_data_key.get (objfile); @@ -348,11 +346,7 @@ block_to_block_object (const struct block *block, struct objfile *objfile) block_object *result = (block_object *) htab_find_with_hash (table, block, hash); if (result != nullptr) - { - PyObject *py_result = (PyObject *) result; - Py_INCREF (py_result); - return py_result; - } + return gdbpy_ref<>::new_reference ((PyObject *) result); result = PyObject_New (block_object, &block_object_type); if (result == nullptr) @@ -364,7 +358,7 @@ block_to_block_object (const struct block *block, struct objfile *objfile) void **slot = htab_find_slot_with_hash (table, block, hash, INSERT); *slot = result; - return (PyObject *) result; + return gdbpy_ref<> (result); } /* Return struct block reference that is wrapped by this object. */ diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c index e98edd52cfb..d9af3639d29 100644 --- a/gdb/python/py-frame.c +++ b/gdb/python/py-frame.c @@ -323,7 +323,8 @@ frapy_block (PyObject *self, PyObject *args) return NULL; } - return block_to_block_object (block, fn_block->function ()->objfile ()); + return block_to_block_object (block, + fn_block->function ()->objfile ()).release (); } diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index 8b5ada2dc2e..587bcf1b8c3 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -522,7 +522,7 @@ pspy_block_for_pc (PyObject *o, PyObject *args) Py_RETURN_NONE; if (block) - return block_to_block_object (block, cust->objfile ()); + return block_to_block_object (block, cust->objfile ()).release (); Py_RETURN_NONE; } diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c index 3b05721e616..550f287a6ee 100644 --- a/gdb/python/py-symtab.c +++ b/gdb/python/py-symtab.c @@ -188,7 +188,8 @@ stpy_global_block (PyObject *self, PyObject *args) blockvector = symtab->compunit ()->blockvector (); const struct block *block = blockvector->global_block (); - return block_to_block_object (block, symtab->compunit ()->objfile ()); + return block_to_block_object (block, + symtab->compunit ()->objfile ()).release (); } /* Return the STATIC_BLOCK of the underlying symtab. */ @@ -204,7 +205,8 @@ stpy_static_block (PyObject *self, PyObject *args) blockvector = symtab->compunit ()->blockvector (); const struct block *block = blockvector->static_block (); - return block_to_block_object (block, symtab->compunit ()->objfile ()); + return block_to_block_object (block, + symtab->compunit ()->objfile ()).release (); } /* Implementation of gdb.Symtab.linetable (self) -> gdb.LineTable. diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c index a71aaac7335..5465d3d6cf9 100644 --- a/gdb/python/py-unwind.c +++ b/gdb/python/py-unwind.c @@ -666,7 +666,8 @@ pending_framepy_block (PyObject *self, PyObject *args) return nullptr; } - return block_to_block_object (block, fn_block->function ()->objfile ()); + return block_to_block_object (block, + fn_block->function ()->objfile ()).release (); } /* Implement gdb.PendingFrame.function(). Return a gdb.Symbol diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 8d101e8afb6..de4226868dc 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -498,8 +498,8 @@ PyObject *gdbpy_register_tui_window (PyObject *self, PyObject *args, gdbpy_ref<> symtab_and_line_to_sal_object (struct symtab_and_line sal); gdbpy_ref<> symtab_to_symtab_object (struct symtab *symtab); gdbpy_ref<> symbol_to_symbol_object (struct symbol *sym); -PyObject *block_to_block_object (const struct block *block, - struct objfile *objfile); +gdbpy_ref<> block_to_block_object (const struct block *block, + struct objfile *objfile); PyObject *value_to_value_object (struct value *v); PyObject *type_to_type_object (struct type *); PyObject *frame_info_to_frame_object (const frame_info_ptr &frame); -- 2.53.0