From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 31F1F385828E for ; Fri, 17 Feb 2023 22:44:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 31F1F385828E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676673863; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=S1ixLd2O4MGFks2NgFMJ12LZDSbT9a4adtnAn5mgUOk=; b=UIfDb8DF/oHZ5lx4KhzKxzLiGOEMYxD4PV2PLf67w7SxpxPlZolI2ShKcqgzBV6zkyqq8f 0erQXZw1skMlmrcvjRiI7GZvOp1PAJi/kPxKK/I6B5zRLvIUb7TO27HvCOSAYbS+goiRzP H53fBV0MlVg3ElUBMqeMTingwgnJuSI= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-31-c_fHdk1sPaCmF2vHHZTRsA-1; Fri, 17 Feb 2023 17:44:22 -0500 X-MC-Unique: c_fHdk1sPaCmF2vHHZTRsA-1 Received: by mail-wm1-f70.google.com with SMTP id m40-20020a05600c3b2800b003dc5cb10dcfso1278533wms.9 for ; Fri, 17 Feb 2023 14:44:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:message-id:date:references:in-reply-to:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=S1ixLd2O4MGFks2NgFMJ12LZDSbT9a4adtnAn5mgUOk=; b=h04lt5WDH3Agzs59KW28uw/jq6dJ1bjiouszXo1id/ZiZo+GYsmGQIIXrd0LxGmeHf jNO4JwRSlgRMCg5I2pW4dDhnD2ecdL2/4/LG9/8h2J5r6RApoBQoibpaByjaQRxpLHBU jMRxr7rSgGIxKwg3nqqCvhH9ovE5NN5wZuCFkBvzCqUN/F5WqMSEW9ldwxKW7piGvcV3 7LckQXniOqGMRKF169gjxZRnd2APgjLxzCvs4hME6B6a8dksnz7l7wLsd6bUCh4AH+Px jM8HlJSLHBxSYkmlW+NyEZrbCnUOEWG5hgr0CSbPhojR0T2+RL90Suj/XO+YNpQya++B 2a5w== X-Gm-Message-State: AO0yUKX1B0/z84ici1gX80i7kzDqYd8gX9cmJnNQrCKoAmmoF8hZMt2r wDcCA71hzVt3ovFSljJ0KizzZmUhGeBRV7BLNTOYoquxgqS068aDFqTJCCPA1IEKB0L22kaXkTh ecFDEay7bz31QmtlX8+PXFA== X-Received: by 2002:a05:600c:1693:b0:3e0:99:b5ac with SMTP id k19-20020a05600c169300b003e00099b5acmr1213393wmn.3.1676673861352; Fri, 17 Feb 2023 14:44:21 -0800 (PST) X-Google-Smtp-Source: AK7set+ir6yJW7eAAX5YOsywGhdJzcO5a/T7hHN62gmJ8fHTXDlGqhYIN4YpNAfcxaEKvvM9I8DeLg== X-Received: by 2002:a05:600c:1693:b0:3e0:99:b5ac with SMTP id k19-20020a05600c169300b003e00099b5acmr1213386wmn.3.1676673861015; Fri, 17 Feb 2023 14:44:21 -0800 (PST) Received: from localhost (95.72.115.87.dyn.plus.net. [87.115.72.95]) by smtp.gmail.com with ESMTPSA id z8-20020a1c4c08000000b003e1f319b87bsm6460388wmf.24.2023.02.17.14.44.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 14:44:20 -0800 (PST) From: Andrew Burgess To: Tom Tromey , gdb-patches@sourceware.org Subject: Re: [PATCH 1/2] Avoid manual memory management in go-lang.c In-Reply-To: <20230216-submit-go-unique-ptr-v1-1-73dd8065c6a6@tromey.com> References: <20230216-submit-go-unique-ptr-v1-0-73dd8065c6a6@tromey.com> <20230216-submit-go-unique-ptr-v1-1-73dd8065c6a6@tromey.com> Date: Fri, 17 Feb 2023 22:44:19 +0000 Message-ID: <87edqnlxoc.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,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 List-Id: Tom Tromey writes: > I noticed a couple of spots in go-lang.c that could be improved by > using unique_ptr. > --- > 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); Maybe replace NULL with nullptr in these comment. But otherwise, this all looks great. Reviewed-By: Andrew Burgess Thanks, Andrew > > extern const struct builtin_go_type *builtin_go_type (struct gdbarch *); > > > -- > 2.39.1