From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 082863858D32; Sat, 18 Feb 2023 00:06:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 082863858D32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1676678784; bh=Qqafw9/RMXOC+u8hLVIpK0lpwj39s1N2Cjm1ta+WhH0=; h=From:To:Subject:Date:From; b=J8o84WybvIyz0ZObQDOsldtYouUlBePr/MOdodZuBdB6gOh7PeK6bS7rbVzChopzX x2XSzxiMy8N9qY9M4myVbPpYaEsrh83hMQnuYeSenbCuvaCVJMlXq7p5A5wNzgDMEQ IJpIH55yXgJADCJHoBhTSGf9Q8YewFZDqt+eAYMM= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Avoid manual memory management in go-lang.c X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 22e1578cc831ab97a34d8bddcba73544a4fce6b8 X-Git-Newrev: be643e074f3081b537a11c87c1ccf1d400f2945e Message-Id: <20230218000624.082863858D32@sourceware.org> Date: Sat, 18 Feb 2023 00:06:23 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dbe643e074f30= 81b537a11c87c1ccf1d400f2945e commit be643e074f3081b537a11c87c1ccf1d400f2945e Author: Tom Tromey Date: Thu Feb 16 17:36:29 2023 -0700 Avoid manual memory management in go-lang.c =20 I noticed a couple of spots in go-lang.c that could be improved by using unique_ptr. =20 Reviewed-By: Andrew Burgess Diff: --- 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 () =3D=3D LOC_BLOCK) { gdb::unique_xmalloc_ptr this_package_name - (go_symbol_package_name (sym)); + =3D go_symbol_package_name (sym); =20 if (this_package_name =3D=3D 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, cons= t struct block *block) current package. */ =20 { - char *current_package_name =3D go_block_package_name (block); + gdb::unique_xmalloc_ptr current_package_name + =3D go_block_package_name (block); =20 if (current_package_name !=3D NULL) { struct stoken sval =3D - 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 ()); =20 - xfree (current_package_name); sym =3D 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, =20 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. =20 *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. */ =20 -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") =3D=3D 0) { - char *package =3D xstrdup ("main"); + gdb::unique_xmalloc_ptr package + =3D make_unique_xstrdup ("main"); =20 - *packagep =3D package; + *packagep =3D package.get (); *objectp =3D "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") =3D=3D 0) { - char *package =3D xstrdup ("main"); + gdb::unique_xmalloc_ptr package + =3D make_unique_xstrdup ("main"); =20 - *packagep =3D package; + *packagep =3D package.get (); *objectp =3D "main"; return package; } @@ -261,7 +260,8 @@ unpack_mangled_go_symbol (const char *mangled_name, =20 /* At this point we've decided we have a mangled Go symbol. */ =20 - buf =3D xstrdup (mangled_name); + gdb::unique_xmalloc_ptr result =3D make_unique_xstrdup (mangled_na= me); + buf =3D result.get (); =20 /* Search backwards looking for "N". */ p =3D buf + len; @@ -317,7 +317,7 @@ unpack_mangled_go_symbol (const char *mangled_name, } =20 unpack_package_and_object (buf, packagep, objectp); - return buf; + return result; } =20 /* 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)); } =20 -/* 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. */ =20 -char * +gdb::unique_xmalloc_ptr go_symbol_package_name (const struct symbol *sym) { const char *mangled_name =3D 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; =20 gdb_assert (sym->language () =3D=3D language_go); name_buf =3D 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 =3D=3D NULL) return NULL; - result =3D xstrdup (package_name); - xfree (name_buf); - return result; + return make_unique_xstrdup (package_name); } =20 -/* 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. */ =20 -char * +gdb::unique_xmalloc_ptr go_block_package_name (const struct block *block) { while (block !=3D NULL) @@ -422,7 +417,8 @@ go_block_package_name (const struct block *block) =20 if (function !=3D NULL) { - char *package_name =3D go_symbol_package_name (function); + gdb::unique_xmalloc_ptr package_name + =3D go_symbol_package_name (function); =20 if (package_name !=3D NULL) return package_name; diff --git a/gdb/go-lang.h b/gdb/go-lang.h index 1820b4c9658..3b1aa5c1bbb 100644 --- a/gdb/go-lang.h +++ b/gdb/go-lang.h @@ -63,9 +63,14 @@ extern const char *go_main_name (void); =20 extern enum go_type go_classify_struct_type (struct type *type); =20 -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 nullptr if unknown. */ +extern gdb::unique_xmalloc_ptr go_symbol_package_name + (const struct symbol *sym); + +/* Return the package that BLOCK is in, or nullptr if there isn't + one. */ +extern gdb::unique_xmalloc_ptr go_block_package_name + (const struct block *block); =20 extern const struct builtin_go_type *builtin_go_type (struct gdbarch *);