Avoid manual memory management in go-lang.c

I noticed a couple of spots in go-lang.c that could be improved by
using unique_ptr.

Reviewed-By: Andrew Burgess <aburgess@redhat.com>
This commit is contained in:
Tom Tromey
2023-02-16 17:36:29 -07:00
parent 22e1578cc8
commit be643e074f
4 changed files with 30 additions and 29 deletions

View File

@ -5838,7 +5838,7 @@ fixup_go_packaging (struct dwarf2_cu *cu)
&& sym->aclass () == LOC_BLOCK) && sym->aclass () == LOC_BLOCK)
{ {
gdb::unique_xmalloc_ptr<char> this_package_name gdb::unique_xmalloc_ptr<char> this_package_name
(go_symbol_package_name (sym)); = go_symbol_package_name (sym);
if (this_package_name == NULL) if (this_package_name == NULL)
continue; continue;

View File

@ -1396,16 +1396,16 @@ classify_name (struct parser_state *par_state, const struct block *block)
current package. */ current package. */
{ {
char *current_package_name = go_block_package_name (block); gdb::unique_xmalloc_ptr<char> current_package_name
= go_block_package_name (block);
if (current_package_name != NULL) if (current_package_name != NULL)
{ {
struct stoken sval = struct stoken sval =
build_packaged_name (current_package_name, build_packaged_name (current_package_name.get (),
strlen (current_package_name), strlen (current_package_name.get ()),
copy.c_str (), copy.size ()); copy.c_str (), copy.size ());
xfree (current_package_name);
sym = lookup_symbol (sval.ptr, block, VAR_DOMAIN, sym = lookup_symbol (sval.ptr, block, VAR_DOMAIN,
&is_a_field_of_this); &is_a_field_of_this);
if (sym.symbol) if (sym.symbol)

View File

@ -163,11 +163,8 @@ unpack_package_and_object (char *buf,
Space for the resulting strings is malloc'd in one buffer. Space for the resulting strings is malloc'd in one buffer.
PACKAGEP,OBJECTP,METHOD_TYPE* will (typically) point into this 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 A pointer to this buffer is returned, or NULL if symbol isn't a
mangled Go symbol. mangled Go symbol.
The caller is responsible for freeing the result.
*METHOD_TYPE_IS_POINTERP is set to a boolean indicating if *METHOD_TYPE_IS_POINTERP is set to a boolean indicating if
the method type is a pointer. 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 If we ever need to unpack the method type, this routine should work
for that too. */ for that too. */
static char * static gdb::unique_xmalloc_ptr<char>
unpack_mangled_go_symbol (const char *mangled_name, unpack_mangled_go_symbol (const char *mangled_name,
const char **packagep, const char **packagep,
const char **objectp, const char **objectp,
@ -209,9 +206,10 @@ unpack_mangled_go_symbol (const char *mangled_name,
/* main.init is mangled specially. */ /* main.init is mangled specially. */
if (strcmp (mangled_name, "__go_init_main") == 0) if (strcmp (mangled_name, "__go_init_main") == 0)
{ {
char *package = xstrdup ("main"); gdb::unique_xmalloc_ptr<char> package
= make_unique_xstrdup ("main");
*packagep = package; *packagep = package.get ();
*objectp = "init"; *objectp = "init";
return package; return package;
} }
@ -219,9 +217,10 @@ unpack_mangled_go_symbol (const char *mangled_name,
/* main.main is mangled specially (missing prefix). */ /* main.main is mangled specially (missing prefix). */
if (strcmp (mangled_name, "main.main") == 0) if (strcmp (mangled_name, "main.main") == 0)
{ {
char *package = xstrdup ("main"); gdb::unique_xmalloc_ptr<char> package
= make_unique_xstrdup ("main");
*packagep = package; *packagep = package.get ();
*objectp = "main"; *objectp = "main";
return package; 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. */ /* At this point we've decided we have a mangled Go symbol. */
buf = xstrdup (mangled_name); gdb::unique_xmalloc_ptr<char> result = make_unique_xstrdup (mangled_name);
buf = result.get ();
/* Search backwards looking for "N<digit(s)>". */ /* Search backwards looking for "N<digit(s)>". */
p = buf + len; p = buf + len;
@ -317,7 +317,7 @@ unpack_mangled_go_symbol (const char *mangled_name,
} }
unpack_package_and_object (buf, packagep, objectp); unpack_package_and_object (buf, packagep, objectp);
return buf; return result;
} }
/* Implements the la_demangle language_defn routine for language Go. /* 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)); return make_unique_xstrdup ((const char *) obstack_finish (&tempbuf));
} }
/* Given a Go symbol, return its package or NULL if unknown. /* See go-lang.h. */
Space for the result is malloc'd, caller must free. */
char * gdb::unique_xmalloc_ptr<char>
go_symbol_package_name (const struct symbol *sym) go_symbol_package_name (const struct symbol *sym)
{ {
const char *mangled_name = sym->linkage_name (); 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_package_name;
const char *method_type_object_name; const char *method_type_object_name;
int method_type_is_pointer; int method_type_is_pointer;
char *name_buf; gdb::unique_xmalloc_ptr<char> name_buf;
char *result;
gdb_assert (sym->language () == language_go); gdb_assert (sym->language () == language_go);
name_buf = unpack_mangled_go_symbol (mangled_name, 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). */ /* Some Go symbols don't have mangled form we interpret (yet). */
if (name_buf == NULL) if (name_buf == NULL)
return NULL; return NULL;
result = xstrdup (package_name); return make_unique_xstrdup (package_name);
xfree (name_buf);
return result;
} }
/* Return the package that BLOCK is in, or NULL if there isn't one. /* See go-lang.h. */
Space for the result is malloc'd, caller must free. */
char * gdb::unique_xmalloc_ptr<char>
go_block_package_name (const struct block *block) go_block_package_name (const struct block *block)
{ {
while (block != NULL) while (block != NULL)
@ -422,7 +417,8 @@ go_block_package_name (const struct block *block)
if (function != NULL) if (function != NULL)
{ {
char *package_name = go_symbol_package_name (function); gdb::unique_xmalloc_ptr<char> package_name
= go_symbol_package_name (function);
if (package_name != NULL) if (package_name != NULL)
return package_name; return package_name;

View File

@ -63,9 +63,14 @@ extern const char *go_main_name (void);
extern enum go_type go_classify_struct_type (struct type *type); extern enum go_type go_classify_struct_type (struct type *type);
extern char *go_symbol_package_name (const struct symbol *sym); /* Given a Go symbol, return its package or nullptr if unknown. */
extern gdb::unique_xmalloc_ptr<char> go_symbol_package_name
(const struct symbol *sym);
extern char *go_block_package_name (const struct block *block); /* Return the package that BLOCK is in, or nullptr if there isn't
one. */
extern gdb::unique_xmalloc_ptr<char> go_block_package_name
(const struct block *block);
extern const struct builtin_go_type *builtin_go_type (struct gdbarch *); extern const struct builtin_go_type *builtin_go_type (struct gdbarch *);