gdb/guile: remove some manual xfree calls

Message ID 7b0c91bf5633e8c51c80da3df3ce1310367000e2.1685566126.git.aburgess@redhat.com
State New
Headers
Series gdb/guile: remove some manual xfree calls |

Commit Message

Andrew Burgess May 31, 2023, 8:49 p.m. UTC
  While working on another patch I had reason to look at
gdbscm_lookup_type, and noticed that we were making some manual calls
to xfree.

This commit switches to using gdb::unique_xmalloc_ptr<char> which
should automate the xfree calls.  I don't believe that we were leaking
memory before this commit -- this is just a cleanup.

There should be no user visible changes after this commit.
---
 gdb/guile/scm-type.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)


base-commit: e9683acf5e51c2bac8aa68d30d9ac3683dddcc7d
  

Comments

Tom Tromey June 2, 2023, 4:43 p.m. UTC | #1
>>>>> "Andrew" == Andrew Burgess via Gdb-patches <gdb-patches@sourceware.org> writes:

Andrew>        if (block == NULL)
Andrew> -	{
Andrew> -	  xfree (name);
Andrew> -	  gdbscm_throw (exception);
Andrew> -	}
Andrew> +	gdbscm_throw (exception);

Unfortunately I don't think this is the same.  IIUC, gdbscm_throw is a
noreturn function that calls longjmp.  So, the destructor will never be
run.

Tom
  
Andrew Burgess June 5, 2023, 9:35 a.m. UTC | #2
Tom Tromey <tom@tromey.com> writes:

>>>>>> "Andrew" == Andrew Burgess via Gdb-patches <gdb-patches@sourceware.org> writes:
>
> Andrew>        if (block == NULL)
> Andrew> -	{
> Andrew> -	  xfree (name);
> Andrew> -	  gdbscm_throw (exception);
> Andrew> -	}
> Andrew> +	gdbscm_throw (exception);
>
> Unfortunately I don't think this is the same.  IIUC, gdbscm_throw is a
> noreturn function that calls longjmp.  So, the destructor will never be
> run.

You are correct.  Thanks for pointing this out.

Thanks,
Andrew
  

Patch

diff --git a/gdb/guile/scm-type.c b/gdb/guile/scm-type.c
index 033b800d8b0..cf2252c62e0 100644
--- a/gdb/guile/scm-type.c
+++ b/gdb/guile/scm-type.c
@@ -1267,15 +1267,16 @@  static SCM
 gdbscm_lookup_type (SCM name_scm, SCM rest)
 {
   SCM keywords[] = { block_keyword, SCM_BOOL_F };
-  char *name;
+  char *name_str;
   SCM block_scm = SCM_BOOL_F;
   int block_arg_pos = -1;
   const struct block *block = NULL;
   struct type *type;
 
   gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, keywords, "s#O",
-			      name_scm, &name,
+			      name_scm, &name_str,
 			      rest, &block_arg_pos, &block_scm);
+  gdb::unique_xmalloc_ptr<char> name (name_str);
 
   if (block_arg_pos != -1)
     {
@@ -1284,13 +1285,9 @@  gdbscm_lookup_type (SCM name_scm, SCM rest)
       block = bkscm_scm_to_block (block_scm, block_arg_pos, FUNC_NAME,
 				  &exception);
       if (block == NULL)
-	{
-	  xfree (name);
-	  gdbscm_throw (exception);
-	}
+	gdbscm_throw (exception);
     }
-  type = tyscm_lookup_typename (name, block);
-  xfree (name);
+  type = tyscm_lookup_typename (name.get (), block);
 
   if (type != NULL)
     return tyscm_scm_from_type (type);