From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id AS20AF7NmGnGNAMAWB0awg (envelope-from ) for ; Fri, 20 Feb 2026 16:08:46 -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=SrJO0i0H; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id EC9851E0BA; Fri, 20 Feb 2026 16:08:45 -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 B11241E08D for ; Fri, 20 Feb 2026 16:08:44 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 4924C4B9DB66 for ; Fri, 20 Feb 2026 21:08:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4924C4B9DB66 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=SrJO0i0H Received: from mail-oa1-x2a.google.com (mail-oa1-x2a.google.com [IPv6:2001:4860:4864:20::2a]) by sourceware.org (Postfix) with ESMTPS id BB3F44BAD148 for ; Fri, 20 Feb 2026 21:03:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BB3F44BAD148 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 BB3F44BAD148 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:4860:4864:20::2a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771621434; cv=none; b=YYFlFhd+qHG8gMRqspAbHX7hvYoauKWoZ9BaozlC+4+YudRSyaLC9+M6Xi91w1upmtHxz/6lBGQAp5YNKWnqb/zwKTldjb3Tklok4SCcP22J/Ei8YvTA3WRyZAjlz9fIRdmqUwAkby9Zise8idLiBUeQx1iNHG2g0Gx9dz+PkL8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771621434; c=relaxed/simple; bh=ps5LlKkvTBEJUd+AgOWgp0MlxjABCfK+77+vi75GJPs=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=OXdJogAmY/ZZe79EKFkMHsOyRe2tvuGoMm0SygxQWJW27BGw9OAJJ4JssSM4J3WzpetLxXatDy2/8Z4RD6RxgDWirWTYr2Vb2FOUAZIPn9N7WLlaEIiKTazhjSut0xA3Xx1KTd8AXHT8VPoWZrY39XAjhsdMHlL7mszkfDMKbQw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BB3F44BAD148 Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-40ede943bf0so1483731fac.2 for ; Fri, 20 Feb 2026 13:03:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1771621434; x=1772226234; 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=K6igUbD8dxuUUnplEkJ1wiAotlrp6eCEMc9abpFMn/c=; b=SrJO0i0Hs6um8vNEtbvzVcu3FZqx7f2GA3pGjC6rH7cj0NOw1bpKm+CdHxbbFKFES4 +hzSI41whKfPNwP09Ko/NsP8sSvFBIoohzayDWbUtz9w+zlxSombm6J2W/qiNWo3sbem Lr2AhyxbfUJyIky+c+95yrESrNjj1tOAVRpzytClabkcIdcYyGgOcgZsgsHIgbVgLOot 1Uxj5mQbrd+6OXSNbiQyeVbszShfKPvZnfpJbZ7GfHqcVP6eaZt/gVLfmkeSa67FHE/C Gqa8/W2Lh5gnbUFje0Cucy6+rtVb9Xh1V0QzE90eGs4nL1nH8J1WrXsODMC1D3ZF9p5V xiPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771621434; x=1772226234; 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=K6igUbD8dxuUUnplEkJ1wiAotlrp6eCEMc9abpFMn/c=; b=ZMfs97fThte2Kfb8JQINhJTKQkS0AXrR2nlWr57zFhginMrYcLOtkPeOQveKS7G/q9 Yjl8haLpxMtVQqeNBBnm/y/oTLSgIcr06WV2jCt+AozO3knWf77mWlf5cCoOJbrW0y24 rVwvcNocQygUlnlH/bKmZXF4D8Hhl2agH9UINb7CeWe8F+9IdmoG4ozJznH1RDzBZv68 hddj/DCY1UcmOCYsLbCB2cJ7IRK9sN75T+Aij7tqt9yzICa7V7V3O8k7QIp1r0EzN+NO Fmq5wgxAivz3zaJKUrXoGcKcrmR9u8034o3MKXoIPHpSsJMJJk1DK4M88qtyUCf2fvqH eNJg== X-Gm-Message-State: AOJu0YyGj9uqvouo/+DPDZwl9wjg9Fak2F9VSAHEyWai85gpadAaLy6P SSlKWP/n92287z5x29H/dBSaU9oPSBWdb00FD2+J78z7aDwcBe7ZKvHbjHEM34civC8ApbdgbXV XZWk= X-Gm-Gg: AZuq6aIjPvscIjeskdu4gBfmE2WXH/SjgMS3R6zTCHSFVW+L97RaMWbJLXRyt5ENCVl Ig/qfgToelCEWJuOvB294D2bIBRFR08GY1HJ/HHVjtEL2rc5sHqjnWrasUXiRyYB9waQKiJ3ick 2CCANh3vozwq6HmoMLp6N5G4elcVdJHzko2VCgadfvJ74raIOglCBmVHaDXMRJmvyeo2vzP/3Yo VGBj+DcoCSeI+atFH4kjWSHApGEu+Cwwl+Q364lofdPWPkKEa5QxyurpTU6BzCwLGlmDOj5yt6x N2o0wicwNOrzT+csOv8WgLcTv64qwrDj/IXjKZYgJeqJcBsl0OyGr8c0amGHaNd9eZGptiBLcHQ eBX/XYe6OqRr88QEyBHy90aE1SQDZYcflId5ZSahP1SUgJV9lpstPNFumW8nFMVv2VpnzsHkiPt Ojx50ngg3KIKM8Jm3Z/j7DXT/3JU0qZdos7/eHQEf1hOVqIumKGaMfTROR X-Received: by 2002:a05:687c:40a1:b0:3ec:565a:13a4 with SMTP id 586e51a60fabf-4157b0f57d3mr660435fac.34.1771621433866; Fri, 20 Feb 2026 13:03:53 -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.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Feb 2026 13:03:53 -0800 (PST) From: Tom Tromey Date: Fri, 20 Feb 2026 14:03:52 -0700 Subject: [PATCH 06/10] Return gdbpy_ref<> from type_to_type_object MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260220-python-safety-minor-v1-6-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 type_to_type_object to return a gdbpy_ref<>, using the type system to convey that a new reference is always returned. --- gdb/python/py-arch.c | 4 ++-- gdb/python/py-lazy-string.c | 2 +- gdb/python/py-symbol.c | 2 +- gdb/python/py-type.c | 45 ++++++++++++++++++++++---------------------- gdb/python/py-value.c | 4 ++-- gdb/python/py-xmethods.c | 2 +- gdb/python/python-internal.h | 2 +- gdb/python/python.c | 2 +- 8 files changed, 31 insertions(+), 32 deletions(-) diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c index eb4a7c63530..cf740821dfa 100644 --- a/gdb/python/py-arch.c +++ b/gdb/python/py-arch.c @@ -314,7 +314,7 @@ archpy_integer_type (PyObject *self, PyObject *args, PyObject *kw) return nullptr; } - return type_to_type_object (type); + return type_to_type_object (type).release (); } /* Implementation of gdb.void_type. */ @@ -324,7 +324,7 @@ archpy_void_type (PyObject *self, PyObject *args) struct gdbarch *gdbarch; ARCHPY_REQUIRE_VALID (self, gdbarch); - return type_to_type_object (builtin_type (gdbarch)->builtin_void); + return type_to_type_object (builtin_type (gdbarch)->builtin_void).release (); } /* __repr__ implementation for gdb.Architecture. */ diff --git a/gdb/python/py-lazy-string.c b/gdb/python/py-lazy-string.c index fe248f6e78f..26998064206 100644 --- a/gdb/python/py-lazy-string.c +++ b/gdb/python/py-lazy-string.c @@ -235,7 +235,7 @@ gdbpy_create_lazy_string_object (CORE_ADDR address, long length, str_obj->encoding = NULL; else str_obj->encoding = xstrdup (encoding); - str_obj->type = type_to_type_object (type); + str_obj->type = type_to_type_object (type).release (); return (PyObject *) str_obj; } diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c index 80c7705aa68..7b6164fb20d 100644 --- a/gdb/python/py-symbol.c +++ b/gdb/python/py-symbol.c @@ -73,7 +73,7 @@ sympy_get_type (PyObject *self, void *closure) return Py_None; } - return type_to_type_object (symbol->type ()); + return type_to_type_object (symbol->type ()).release (); } static PyObject * diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c index 017937c1c0a..8f8f6e326a8 100644 --- a/gdb/python/py-type.c +++ b/gdb/python/py-type.c @@ -132,7 +132,7 @@ convert_field (struct type *type, int field) if (result == NULL) return NULL; - gdbpy_ref<> arg (type_to_type_object (type)); + gdbpy_ref<> arg = type_to_type_object (type); if (arg == NULL) return NULL; if (PyObject_SetAttrString (result.get (), "parent_type", arg.get ()) < 0) @@ -202,7 +202,7 @@ convert_field (struct type *type, int field) if (type->field (field).type () == NULL) arg = gdbpy_ref<>::new_reference (Py_None); else - arg.reset (type_to_type_object (type->field (field).type ())); + arg = type_to_type_object (type->field (field).type ()); if (arg == NULL) return NULL; if (PyObject_SetAttrString (result.get (), "type", arg.get ()) < 0) @@ -283,7 +283,7 @@ typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind) gdbpy_ref<> type_holder; if (checked_type != type) { - type_holder.reset (type_to_type_object (checked_type)); + type_holder = type_to_type_object (checked_type); if (type_holder == nullptr) return nullptr; py_type = type_holder.get (); @@ -489,7 +489,7 @@ typy_strip_typedefs (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return type_to_type_object (type); + return type_to_type_object (type).release (); } /* Strip typedefs and pointers/reference from a type. Then check that @@ -580,7 +580,7 @@ typy_array_1 (PyObject *self, PyObject *args, int is_vector) return gdbpy_handle_gdb_exception (nullptr, except); } - return type_to_type_object (array); + return type_to_type_object (array).release (); } /* Return an array type. */ @@ -614,7 +614,7 @@ typy_pointer (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return type_to_type_object (type); + return type_to_type_object (type).release (); } /* Return the range of a type represented by SELF. The return type is @@ -686,7 +686,7 @@ typy_reference (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return type_to_type_object (type); + return type_to_type_object (type).release (); } /* Return a Type object which represents the target type of SELF. */ @@ -702,7 +702,7 @@ typy_target (PyObject *self, PyObject *args) return NULL; } - return type_to_type_object (type->target_type ()); + return type_to_type_object (type->target_type ()).release (); } /* Return a const-qualified type variant. */ @@ -720,7 +720,7 @@ typy_const (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return type_to_type_object (type); + return type_to_type_object (type).release (); } /* Return a volatile-qualified type variant. */ @@ -738,7 +738,7 @@ typy_volatile (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return type_to_type_object (type); + return type_to_type_object (type).release (); } /* Return an unqualified type variant. */ @@ -756,7 +756,7 @@ typy_unqualified (PyObject *self, PyObject *args) return gdbpy_handle_gdb_exception (nullptr, except); } - return type_to_type_object (type); + return type_to_type_object (type).release (); } /* Return the size of the type represented by SELF, in bytes. */ @@ -978,7 +978,7 @@ typy_legacy_template_argument (struct type *type, const struct block *block, if (! argtype) return NULL; - return type_to_type_object (argtype); + return type_to_type_object (argtype).release (); } static PyObject * @@ -1037,7 +1037,7 @@ typy_template_argument (PyObject *self, PyObject *args) sym = TYPE_TEMPLATE_ARGUMENT (type, argno); if (sym->loc_class () == LOC_TYPEDEF) - return type_to_type_object (sym->type ()); + return type_to_type_object (sym->type ()).release (); else if (sym->loc_class () == LOC_OPTIMIZED_OUT) { PyErr_Format (PyExc_RuntimeError, @@ -1423,7 +1423,7 @@ typy_iterator_dealloc (PyObject *obj) } /* Create a new Type referring to TYPE. */ -PyObject * +gdbpy_ref<> type_to_type_object (struct type *type) { type_object *type_obj; @@ -1450,14 +1450,13 @@ type_to_type_object (struct type *type) else type_obj = typy_registry.lookup (type->arch_owner (), type); - if (type_obj != nullptr) - return (PyObject*)type_obj; - - type_obj = PyObject_New (type_object, &type_object_type); - if (type_obj) - set_type (type_obj, type); - - return (PyObject *) type_obj; + if (type_obj == nullptr) + { + type_obj = PyObject_New (type_object, &type_object_type); + if (type_obj) + set_type (type_obj, type); + } + return gdbpy_ref<> (type_obj); } struct type * @@ -1499,7 +1498,7 @@ gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw) if (! type) return NULL; - return type_to_type_object (type); + return type_to_type_object (type).release (); } static int diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index c0fcfb882e1..a5b2303728c 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -432,7 +432,7 @@ valpy_get_type (PyObject *self, void *closure) { value_object *obj = (value_object *) self; - return type_to_type_object (obj->value->type ()); + return type_to_type_object (obj->value->type ()).release (); } /* Return dynamic type of the value. */ @@ -484,7 +484,7 @@ valpy_get_dynamic_type (PyObject *self, void *closure) return gdbpy_handle_gdb_exception (nullptr, except); } - return type_to_type_object (type); + return type_to_type_object (type).release (); } /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) -> diff --git a/gdb/python/py-xmethods.c b/gdb/python/py-xmethods.c index 64f9807cfee..1fafafb4d24 100644 --- a/gdb/python/py-xmethods.c +++ b/gdb/python/py-xmethods.c @@ -123,7 +123,7 @@ gdbpy_get_matching_xmethod_workers gdbpy_enter enter_py; - gdbpy_ref<> py_type (type_to_type_object (obj_type)); + gdbpy_ref<> py_type = type_to_type_object (obj_type); if (py_type == NULL) { gdbpy_print_stack (); diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index c7e812d6893..6fa37cf85bd 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -501,7 +501,7 @@ gdbpy_ref<> symbol_to_symbol_object (struct symbol *sym); gdbpy_ref<> block_to_block_object (const struct block *block, struct objfile *objfile); gdbpy_ref<> value_to_value_object (struct value *v); -PyObject *type_to_type_object (struct type *); +gdbpy_ref<> type_to_type_object (struct type *); PyObject *frame_info_to_frame_object (const frame_info_ptr &frame); PyObject *symtab_to_linetable_object (PyObject *symtab); gdbpy_ref<> pspace_to_pspace_object (struct program_space *); diff --git a/gdb/python/python.c b/gdb/python/python.c index aec2c7559a8..9862d1a726f 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -2105,7 +2105,7 @@ gdbpy_apply_type_printers (const struct extension_language_defn *extlang, gdbpy_enter enter_py; - gdbpy_ref<> type_obj (type_to_type_object (type)); + gdbpy_ref<> type_obj = type_to_type_object (type); if (type_obj == NULL) { gdbpy_print_stack (); -- 2.53.0