From patchwork Fri Feb 17 01:00:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 65143 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AF36A385B514 for ; Fri, 17 Feb 2023 01:01:37 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from qproxy6-pub.mail.unifiedlayer.com (qproxy6-pub.mail.unifiedlayer.com [69.89.23.12]) by sourceware.org (Postfix) with ESMTPS id 287693858D33 for ; Fri, 17 Feb 2023 01:01:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 287693858D33 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from outbound-ss-761.bluehost.com (outbound-ss-761.bluehost.com [74.220.211.250]) by qproxy6.mail.unifiedlayer.com (Postfix) with ESMTP id 5F1C1802B614 for ; Fri, 17 Feb 2023 01:01:19 +0000 (UTC) Received: from cmgw12.mail.unifiedlayer.com (unknown [10.0.90.127]) by progateway8.mail.pro1.eigbox.com (Postfix) with ESMTP id 4B5761004CA5D for ; Fri, 17 Feb 2023 01:00:19 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id Sp6ppfGMpFh3zSp6pp4q8G; Fri, 17 Feb 2023 01:00:19 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=QpabYX+d c=1 sm=1 tr=0 ts=63eed1a3 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=IkcTkHD0fZMA:10:nop_charset_1 a=m04uMKEZRckA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=Bq1L1D-sWdQ0042_rlEA:9 a=QEXdDO2ut3YA:10:nop_charset_2 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:Date:From:Sender:Reply-To:Cc: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=h9Mp+34DQQlyY6eWVfQ9A0ZNEVhDIRWqJ6u6rivn5y4=; b=yiI4j8S7IUYCrcsR7cp8urDVMM m7a5XaTO90HbM6vm9XjsEx8zwB8Jlibjb7g8oo+k/f4l/jbvff5s1eq0iZB4fNVJLtXak2Z79uABQ YCddsv8vx7uDDvPWo64dAIkRa; Received: from c-98-245-160-18.hsd1.co.comcast.net ([98.245.160.18]:33030 helo=[192.168.0.59]) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pSp6o-000gn4-Tm for gdb-patches@sourceware.org; Thu, 16 Feb 2023 18:00:18 -0700 From: Tom Tromey Date: Thu, 16 Feb 2023 18:00:12 -0700 Subject: [PATCH 1/2] Avoid manual memory management in go-lang.c MIME-Version: 1.0 Message-Id: <20230216-submit-go-unique-ptr-v1-1-73dd8065c6a6@tromey.com> References: <20230216-submit-go-unique-ptr-v1-0-73dd8065c6a6@tromey.com> In-Reply-To: <20230216-submit-go-unique-ptr-v1-0-73dd8065c6a6@tromey.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.0 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: 98.245.160.18 X-Source-L: No X-Exim-ID: 1pSp6o-000gn4-Tm X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: c-98-245-160-18.hsd1.co.comcast.net ([192.168.0.59]) [98.245.160.18]:33030 X-Source-Auth: tom+tromey.com X-Email-Count: 2 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3026.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 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 Sender: "Gdb-patches" I noticed a couple of spots in go-lang.c that could be improved by using unique_ptr. Reviewed-By: Andrew Burgess --- gdb/dwarf2/read.c | 2 +- gdb/go-exp.y | 8 ++++---- gdb/go-lang.c | 40 ++++++++++++++++++---------------------- gdb/go-lang.h | 11 ++++++++--- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index eb37c776989..4fd46fd43f8 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -5838,7 +5838,7 @@ fixup_go_packaging (struct dwarf2_cu *cu) && sym->aclass () == LOC_BLOCK) { gdb::unique_xmalloc_ptr this_package_name - (go_symbol_package_name (sym)); + = go_symbol_package_name (sym); if (this_package_name == NULL) continue; diff --git a/gdb/go-exp.y b/gdb/go-exp.y index 242e7103b94..6aa4c426df6 100644 --- a/gdb/go-exp.y +++ b/gdb/go-exp.y @@ -1396,16 +1396,16 @@ classify_name (struct parser_state *par_state, const struct block *block) current package. */ { - char *current_package_name = go_block_package_name (block); + gdb::unique_xmalloc_ptr current_package_name + = go_block_package_name (block); if (current_package_name != NULL) { struct stoken sval = - build_packaged_name (current_package_name, - strlen (current_package_name), + build_packaged_name (current_package_name.get (), + strlen (current_package_name.get ()), copy.c_str (), copy.size ()); - xfree (current_package_name); sym = lookup_symbol (sval.ptr, block, VAR_DOMAIN, &is_a_field_of_this); if (sym.symbol) diff --git a/gdb/go-lang.c b/gdb/go-lang.c index 7549f14dc63..f9176ace71d 100644 --- a/gdb/go-lang.c +++ b/gdb/go-lang.c @@ -163,11 +163,8 @@ unpack_package_and_object (char *buf, Space for the resulting strings is malloc'd in one buffer. PACKAGEP,OBJECTP,METHOD_TYPE* will (typically) point into this buffer. - [There are a few exceptions, but the caller is still responsible for - freeing the resulting pointer.] A pointer to this buffer is returned, or NULL if symbol isn't a mangled Go symbol. - The caller is responsible for freeing the result. *METHOD_TYPE_IS_POINTERP is set to a boolean indicating if the method type is a pointer. @@ -180,7 +177,7 @@ unpack_package_and_object (char *buf, If we ever need to unpack the method type, this routine should work for that too. */ -static char * +static gdb::unique_xmalloc_ptr unpack_mangled_go_symbol (const char *mangled_name, const char **packagep, const char **objectp, @@ -209,9 +206,10 @@ unpack_mangled_go_symbol (const char *mangled_name, /* main.init is mangled specially. */ if (strcmp (mangled_name, "__go_init_main") == 0) { - char *package = xstrdup ("main"); + gdb::unique_xmalloc_ptr package + = make_unique_xstrdup ("main"); - *packagep = package; + *packagep = package.get (); *objectp = "init"; return package; } @@ -219,9 +217,10 @@ unpack_mangled_go_symbol (const char *mangled_name, /* main.main is mangled specially (missing prefix). */ if (strcmp (mangled_name, "main.main") == 0) { - char *package = xstrdup ("main"); + gdb::unique_xmalloc_ptr package + = make_unique_xstrdup ("main"); - *packagep = package; + *packagep = package.get (); *objectp = "main"; return package; } @@ -261,7 +260,8 @@ unpack_mangled_go_symbol (const char *mangled_name, /* At this point we've decided we have a mangled Go symbol. */ - buf = xstrdup (mangled_name); + gdb::unique_xmalloc_ptr result = make_unique_xstrdup (mangled_name); + buf = result.get (); /* Search backwards looking for "N". */ p = buf + len; @@ -317,7 +317,7 @@ unpack_mangled_go_symbol (const char *mangled_name, } unpack_package_and_object (buf, packagep, objectp); - return buf; + return result; } /* Implements the la_demangle language_defn routine for language Go. @@ -381,10 +381,9 @@ go_language::demangle_symbol (const char *mangled_name, int options) const return make_unique_xstrdup ((const char *) obstack_finish (&tempbuf)); } -/* Given a Go symbol, return its package or NULL if unknown. - Space for the result is malloc'd, caller must free. */ +/* See go-lang.h. */ -char * +gdb::unique_xmalloc_ptr go_symbol_package_name (const struct symbol *sym) { const char *mangled_name = sym->linkage_name (); @@ -393,8 +392,7 @@ go_symbol_package_name (const struct symbol *sym) const char *method_type_package_name; const char *method_type_object_name; int method_type_is_pointer; - char *name_buf; - char *result; + gdb::unique_xmalloc_ptr name_buf; gdb_assert (sym->language () == language_go); name_buf = unpack_mangled_go_symbol (mangled_name, @@ -405,15 +403,12 @@ go_symbol_package_name (const struct symbol *sym) /* Some Go symbols don't have mangled form we interpret (yet). */ if (name_buf == NULL) return NULL; - result = xstrdup (package_name); - xfree (name_buf); - return result; + return make_unique_xstrdup (package_name); } -/* Return the package that BLOCK is in, or NULL if there isn't one. - Space for the result is malloc'd, caller must free. */ +/* See go-lang.h. */ -char * +gdb::unique_xmalloc_ptr go_block_package_name (const struct block *block) { while (block != NULL) @@ -422,7 +417,8 @@ go_block_package_name (const struct block *block) if (function != NULL) { - char *package_name = go_symbol_package_name (function); + gdb::unique_xmalloc_ptr package_name + = go_symbol_package_name (function); if (package_name != NULL) return package_name; diff --git a/gdb/go-lang.h b/gdb/go-lang.h index 1820b4c9658..877b6175534 100644 --- a/gdb/go-lang.h +++ b/gdb/go-lang.h @@ -63,9 +63,14 @@ extern const char *go_main_name (void); extern enum go_type go_classify_struct_type (struct type *type); -extern char *go_symbol_package_name (const struct symbol *sym); - -extern char *go_block_package_name (const struct block *block); +/* Given a Go symbol, return its package or NULL if unknown. */ +extern gdb::unique_xmalloc_ptr go_symbol_package_name + (const struct symbol *sym); + +/* Return the package that BLOCK is in, or NULL if there isn't + one. */ +extern gdb::unique_xmalloc_ptr go_block_package_name + (const struct block *block); extern const struct builtin_go_type *builtin_go_type (struct gdbarch *); From patchwork Fri Feb 17 01:00:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 65142 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C9F83385735E for ; Fri, 17 Feb 2023 01:00:44 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from gproxy4-pub.mail.unifiedlayer.com (gproxy4-pub.mail.unifiedlayer.com [69.89.23.142]) by sourceware.org (Postfix) with ESMTPS id 20F013858C78 for ; Fri, 17 Feb 2023 01:00:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 20F013858C78 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw13.mail.unifiedlayer.com (unknown [10.0.90.128]) by progateway6.mail.pro1.eigbox.com (Postfix) with ESMTP id 75B2E10043685 for ; Fri, 17 Feb 2023 01:00:19 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id Sp6ppysVfNX2aSp6ppj2gP; Fri, 17 Feb 2023 01:00:19 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=NMAQR22g c=1 sm=1 tr=0 ts=63eed1a3 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=IkcTkHD0fZMA:10:nop_charset_1 a=m04uMKEZRckA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=CCpqsmhAAAAA:8 a=mDV3o1hIAAAA:8 a=uWX6QWOKjYWtA4pYIaIA:9 a=QEXdDO2ut3YA:10:nop_charset_2 a=ul9cdbp4aOFLsgKbc677:22 a=_FVE-zBwftR9WsbkzFJk:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:Date:From:Sender:Reply-To:Cc: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=UxvNUqb30jRcgebhwg1ETnrC8VXDdqN1+H+LD3F63Is=; b=nC3SzzoF7YshuhBYib6f+RhuWd eSYDKcOprGX1CSDRohoKYSYvTfXrkluUXnClV8zYQGX5Tprr8Mk3LKIk/N+P158YxXw35xlmipQFR fx9OeqmBh2D0OL10YXuk01aXw; Received: from c-98-245-160-18.hsd1.co.comcast.net ([98.245.160.18]:33030 helo=[192.168.0.59]) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pSp6p-000gn4-5C for gdb-patches@sourceware.org; Thu, 16 Feb 2023 18:00:19 -0700 From: Tom Tromey Date: Thu, 16 Feb 2023 18:00:13 -0700 Subject: [PATCH 2/2] Fix crash in go_symbol_package_name MIME-Version: 1.0 Message-Id: <20230216-submit-go-unique-ptr-v1-2-73dd8065c6a6@tromey.com> References: <20230216-submit-go-unique-ptr-v1-0-73dd8065c6a6@tromey.com> In-Reply-To: <20230216-submit-go-unique-ptr-v1-0-73dd8065c6a6@tromey.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.0 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: 98.245.160.18 X-Source-L: No X-Exim-ID: 1pSp6p-000gn4-5C X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: c-98-245-160-18.hsd1.co.comcast.net ([192.168.0.59]) [98.245.160.18]:33030 X-Source-Auth: tom+tromey.com X-Email-Count: 3 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3026.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 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 Sender: "Gdb-patches" go_symbol_package_name package name asserts that it is only passed a Go symbol, but this is not enforced by one caller. It seems simplest to just check and return early in this case. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=17876 Reviewed-By: Andrew Burgess --- gdb/go-lang.c | 3 ++- gdb/testsuite/gdb.go/no-package.exp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/gdb/go-lang.c b/gdb/go-lang.c index f9176ace71d..bbf80af1f5c 100644 --- a/gdb/go-lang.c +++ b/gdb/go-lang.c @@ -394,7 +394,8 @@ go_symbol_package_name (const struct symbol *sym) int method_type_is_pointer; gdb::unique_xmalloc_ptr name_buf; - gdb_assert (sym->language () == language_go); + if (sym->language () != language_go) + return nullptr; name_buf = unpack_mangled_go_symbol (mangled_name, &package_name, &object_name, &method_type_package_name, diff --git a/gdb/testsuite/gdb.go/no-package.exp b/gdb/testsuite/gdb.go/no-package.exp new file mode 100644 index 00000000000..3b39bc3220d --- /dev/null +++ b/gdb/testsuite/gdb.go/no-package.exp @@ -0,0 +1,31 @@ +# This testcase is part of GDB, the GNU debugger. + +# Copyright 2023 Free Software Foundation, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Regression test for a crash in go_symbol_package_name. + +load_lib "go.exp" + +require allow_go_tests support_go_compile !use_gdb_stub + +standard_testfile integers.go + +if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile} {debug go}]} { + return -1 +} + +# The bug was that stopping in _start would crash. +runto "*_start" message