From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 6WfbM/ey22p10xAAWB0awg (envelope-from ) for ; Fri, 23 Oct 2026 15:18:15 -0400 Authentication-Results: simark.ca; 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=DtwMPlCJ; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id CF13A1E007; Fri, 23 Oct 2026 15:18:15 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_INVALID,DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_BL_SPAMCOP_NET, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=no 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 56CF31E007 for ; Fri, 23 Oct 2026 15:18:15 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E5C344BA2E1B for ; Fri, 23 Oct 2026 19:18:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E5C344BA2E1B 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=DtwMPlCJ Received: from omta34.uswest2.a.cloudfilter.net (omta34.uswest2.a.cloudfilter.net [35.89.44.33]) by sourceware.org (Postfix) with ESMTPS id D983C4BA2E13 for ; Fri, 23 Oct 2026 19:15:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D983C4BA2E13 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 D983C4BA2E13 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=35.89.44.33 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792782953; cv=none; b=KQW9WkMArtsE+7czAQw5gROFM8Al0cZERLuMyCGciHlaI/sv3ZvSJU5/dPYmAd9ayst/3PNUi4clkxbj0SE940YAcjMRiUANsjHxWg4cWTylriyH11G34WBBlzknzxSrhZbRGmWVC+R5CuE1T3DG8uGPozWOTqwbPWyKioKmfqQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792782953; c=relaxed/simple; bh=BL0AWvjT5Nny+4fxGEgYBo9pJc89ozpkLTq2XsM4ASU=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=lp/sH2GG6HA+b9zpD+XXnUpc9Cm3uBVFc3hm0IMH8/403V6VQrrQeRNUEe3R7Ux9/K5hDdNrPwpYS9KaUFfPtSWtwQJ35qk5S3YQMtXWpDbbujnj2LCv7FPO7OyibswjTyEUFDNIWTXfBb+ljxY2TxEO3bH+Un8B2DO1HzF2KAU= 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=DtwMPlCJ reason="signing key too small" DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D983C4BA2E13 Received: from eig-obgw-6006b.ext.cloudfilter.net ([10.0.30.211]) by cmsmtp with ESMTPS id KJZFxbnXRaC0UKKk3xDieV; Fri, 23 Oct 2026 19:15:51 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id KKk3xxBkCaf76KKk3xHzG5; Fri, 23 Oct 2026 19:15:51 +0000 X-Authority-Analysis: v=2.4 cv=WporMcfv c=1 sm=1 tr=0 ts=6adbb267 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=IkcTkHD0fZMA:10 a=660iZSQnnn4A:10 a=ItBw4LHWJt0A:10 a=AYVDQsqcMW1Lf6rGLPQA: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-Unsubscribe-Post:List-Subscribe:List-Post:List-Owner:List-Archive; bh=hgV/tDfSInSpUb34bUKz/jQMMdMdDzklkHaHprXTMlA=; b=DtwMPlCJjbsJsfsY1bEb7zShto YdMef7O13S+tmyD9KQtompywOXx7eoYblSybEw5mPvTNxEVJf6KapEiPCLMAS5Th0mzL7F3oiFhZN kIBzips2Q2Mjskl91rIL6PBMZ; Received: from 97-122-110-129.hlrn.qwest.net ([97.122.110.129]:59452 helo=[192.168.0.17]) by box5379.bluehost.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.100.1) (envelope-from ) id 1xKKk2-00000000DHL-3aEC; Fri, 23 Oct 2026 13:15:50 -0600 From: Tom Tromey Date: Fri, 23 Oct 2026 13:15:50 -0600 Subject: [PATCH 5/6] Safe wrappers for some string conversions MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261023-python-safety-fixmes-v1-5-055b9e1d237b@tromey.com> References: <20261023-python-safety-fixmes-v1-0-055b9e1d237b@tromey.com> In-Reply-To: <20261023-python-safety-fixmes-v1-0-055b9e1d237b@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: 97.122.110.129 X-Source-L: No X-Exim-ID: 1xKKk2-00000000DHL-3aEC X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-110-129.hlrn.qwest.net ([192.168.0.17]) [97.122.110.129]:59452 X-Source-Auth: tom+tromey.com X-Email-Count: 6 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfBycS88fKwZZ5FNgHvJsCpSykBO83Gs1FAM53mZYEBq01Xv88J+3zr0IcAPUIeAMW5Pa3JHHjNhbHKohUxKMVL5k9RJcFG3VAI5kxjbcnMKmXlVZDbvT xYEhY/ue/ma7JzsGbRPAS5tfk1YRLnBgoEde6boMiMRibuv6X0+dO/XZ22F08voIK2SoPZuds9RJRBjWrI4zIs2Z7F/RlT625Dc= 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 adds a couple of new Python wrappers for "safe-style" string conversions. This allows some FIXME removal. Note that I've added a new gdbpy_borrowed_ref constructor, from gdbpy_opt_borrowed_ref. This is checked at runtime to preserve the invariant. --- gdb/python/py-frame.c | 9 ++------- gdb/python/py-ref.h | 7 +++++++ gdb/python/py-tui.c | 7 +------ gdb/python/py-utils.c | 20 ++++++++++++++++++++ gdb/python/python-internal.h | 4 ++++ 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c index e4dd9e9252e..e34d4b5d2e9 100644 --- a/gdb/python/py-frame.c +++ b/gdb/python/py-frame.c @@ -355,13 +355,8 @@ frame_object::read_var (gdbpy_borrowed_ref<> args, var = symbol_object_to_symbol (sym_obj); else if (gdbpy_is_string (sym_obj)) { - gdb::unique_xmalloc_ptr - var_name (python_string_to_target_string (sym_obj)); - - /* FIXME: Python safety. python_string_to_target_string should - throw on error. */ - if (var_name == nullptr) - throw gdb_python_exception (); + gdb::unique_xmalloc_ptr var_name + = safe_python_string_to_target_string (sym_obj); if (block_obj != nullptr) { diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h index 3d0373b7001..7cbfb03b103 100644 --- a/gdb/python/py-ref.h +++ b/gdb/python/py-ref.h @@ -108,6 +108,13 @@ class gdbpy_borrowed_ref : public gdbpy_opt_borrowed_ref gdb_assert (this->m_obj != nullptr); } + template + gdbpy_borrowed_ref (gdbpy_opt_borrowed_ref obj) + : gdbpy_opt_borrowed_ref (obj) + { + gdb_assert (this->m_obj != nullptr); + } + gdbpy_borrowed_ref (std::nullptr_t) = delete; /* Allow a (checked) conversion to any subclass of T. */ diff --git a/gdb/python/py-tui.c b/gdb/python/py-tui.c index 388e441ed55..be8c22aac27 100644 --- a/gdb/python/py-tui.c +++ b/gdb/python/py-tui.c @@ -506,13 +506,8 @@ gdbpy_tui_window::set_title (gdbpy_opt_borrowed_ref<> new_title) gdbpy_err_format (PyExc_TypeError, _("Cannot delete \"title\" attribute.")); - /* FIXME: Python safety. Ideally python_string_to_host_string would - throw on error, but this can't be done until more of the code has - been converted. */ gdb::unique_xmalloc_ptr value - = python_string_to_host_string (new_title); - if (value == nullptr) - throw gdb_python_exception (); + = safe_python_string_to_host_string (new_title); window->set_title (value.get ()); } diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c index 1c68b206601..40433256c62 100644 --- a/gdb/python/py-utils.c +++ b/gdb/python/py-utils.c @@ -119,6 +119,16 @@ python_string_to_target_string (PyObject *obj) return unicode_to_target_string (str.get ()); } +/* Wrapper for python_string_to_target_string that throws on failure. */ +gdb::unique_xmalloc_ptr +safe_python_string_to_target_string (gdbpy_borrowed_ref<> obj) +{ + gdb::unique_xmalloc_ptr result = python_string_to_target_string (obj); + if (result == nullptr) + throw gdb_python_exception (); + return result; +} + /* Converts a python string (8-bit or unicode) to a target string in the target's charset. Returns NULL on error, with a python exception set. @@ -147,6 +157,16 @@ python_string_to_host_string (PyObject *obj) return unicode_to_encoded_string (str.get (), host_charset ()); } +/* Wrapper for python_string_to_host_string that throws on failure. */ +gdb::unique_xmalloc_ptr +safe_python_string_to_host_string (gdbpy_borrowed_ref<> obj) +{ + gdb::unique_xmalloc_ptr result = python_string_to_host_string (obj); + if (result == nullptr) + throw gdb_python_exception (); + return result; +} + /* See python/python-internal.h. */ gdbpy_ref<> diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 5515820411b..ee1a19131bd 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -1017,8 +1017,12 @@ void gdbpy_print_stack_or_quit (); gdbpy_ref<> python_string_to_unicode (PyObject *obj); gdb::unique_xmalloc_ptr unicode_to_target_string (PyObject *unicode_str); gdb::unique_xmalloc_ptr python_string_to_target_string (PyObject *obj); +gdb::unique_xmalloc_ptr safe_python_string_to_target_string + (gdbpy_borrowed_ref<> obj); gdbpy_ref<> python_string_to_target_python_string (PyObject *obj); gdb::unique_xmalloc_ptr python_string_to_host_string (PyObject *obj); +gdb::unique_xmalloc_ptr safe_python_string_to_host_string + (gdbpy_borrowed_ref<> obj); /* Convert a host string STR to a python string. */ -- 2.49.0