public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb: make inferior::args a unique_xmalloc_ptr
@ 2021-05-06 15:07 Simon Marchi
  2021-05-06 16:13 ` Andrew Burgess
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Marchi @ 2021-05-06 15:07 UTC (permalink / raw)
  To: gdb-patches

Use unique_xmalloc_ptr to avoid manual memory management.

gdb/ChangeLog:

	* inferior.h (class inferior) <args>: Change type to
	unique_xmalloc_ptr.
	* inferior.c (inferior::~inferior): Don't free args.
	* infcmd.c (get_inferior_args): Adjust.
	(set_inferior_args): Adjust.

Change-Id: I96300e59eb2faf2d80660416a8f5694d243a944e
---
 gdb/infcmd.c   | 11 +++++++----
 gdb/inferior.c |  1 -
 gdb/inferior.h |  2 +-
 3 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 5aa6b00f20f3..5d9d79261efe 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -136,9 +136,9 @@ get_inferior_args (void)
     }
 
   if (current_inferior ()->args == NULL)
-    current_inferior ()->args = xstrdup ("");
+    current_inferior ()->args = make_unique_xstrdup ("");
 
-  return current_inferior ()->args;
+  return current_inferior ()->args.get ();
 }
 
 /* Set the arguments for the current inferior.  Ownership of
@@ -147,8 +147,11 @@ get_inferior_args (void)
 void
 set_inferior_args (const char *newargs)
 {
-  xfree (current_inferior ()->args);
-  current_inferior ()->args = newargs ? xstrdup (newargs) : NULL;
+  if (newargs != nullptr)
+    current_inferior ()->args = make_unique_xstrdup (newargs);
+  else
+    current_inferior ()->args.reset ();
+
   current_inferior ()->argc = 0;
   current_inferior ()->argv = 0;
 }
diff --git a/gdb/inferior.c b/gdb/inferior.c
index df3b7bf81f02..a8779c354b5d 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -75,7 +75,6 @@ inferior::~inferior ()
 
   m_continuations.clear ();
   inferior_free_data (inf);
-  xfree (inf->args);
   target_desc_info_free (inf->tdesc_info);
 }
 
diff --git a/gdb/inferior.h b/gdb/inferior.h
index e0a7d622ccbb..4a143c3a2b8e 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -468,7 +468,7 @@ class inferior : public refcounted_object
   struct program_space *pspace = NULL;
 
   /* The arguments string to use when running.  */
-  char *args = NULL;
+  gdb::unique_xmalloc_ptr<char> args;
 
   /* The size of elements in argv.  */
   int argc = 0;
-- 
2.30.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] gdb: make inferior::args a unique_xmalloc_ptr
  2021-05-06 15:07 [PATCH] gdb: make inferior::args a unique_xmalloc_ptr Simon Marchi
@ 2021-05-06 16:13 ` Andrew Burgess
  2021-05-06 17:17   ` Simon Marchi
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Burgess @ 2021-05-06 16:13 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

* Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> [2021-05-06 11:07:23 -0400]:

> Use unique_xmalloc_ptr to avoid manual memory management.
> 
> gdb/ChangeLog:
> 
> 	* inferior.h (class inferior) <args>: Change type to
> 	unique_xmalloc_ptr.
> 	* inferior.c (inferior::~inferior): Don't free args.
> 	* infcmd.c (get_inferior_args): Adjust.
> 	(set_inferior_args): Adjust.

LGTM.

Thanks,
Andre
> 
> Change-Id: I96300e59eb2faf2d80660416a8f5694d243a944e
> ---
>  gdb/infcmd.c   | 11 +++++++----
>  gdb/inferior.c |  1 -
>  gdb/inferior.h |  2 +-
>  3 files changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/gdb/infcmd.c b/gdb/infcmd.c
> index 5aa6b00f20f3..5d9d79261efe 100644
> --- a/gdb/infcmd.c
> +++ b/gdb/infcmd.c
> @@ -136,9 +136,9 @@ get_inferior_args (void)
>      }
>  
>    if (current_inferior ()->args == NULL)
> -    current_inferior ()->args = xstrdup ("");
> +    current_inferior ()->args = make_unique_xstrdup ("");
>  
> -  return current_inferior ()->args;
> +  return current_inferior ()->args.get ();
>  }
>  
>  /* Set the arguments for the current inferior.  Ownership of
> @@ -147,8 +147,11 @@ get_inferior_args (void)
>  void
>  set_inferior_args (const char *newargs)
>  {
> -  xfree (current_inferior ()->args);
> -  current_inferior ()->args = newargs ? xstrdup (newargs) : NULL;
> +  if (newargs != nullptr)
> +    current_inferior ()->args = make_unique_xstrdup (newargs);
> +  else
> +    current_inferior ()->args.reset ();
> +
>    current_inferior ()->argc = 0;
>    current_inferior ()->argv = 0;
>  }
> diff --git a/gdb/inferior.c b/gdb/inferior.c
> index df3b7bf81f02..a8779c354b5d 100644
> --- a/gdb/inferior.c
> +++ b/gdb/inferior.c
> @@ -75,7 +75,6 @@ inferior::~inferior ()
>  
>    m_continuations.clear ();
>    inferior_free_data (inf);
> -  xfree (inf->args);
>    target_desc_info_free (inf->tdesc_info);
>  }
>  
> diff --git a/gdb/inferior.h b/gdb/inferior.h
> index e0a7d622ccbb..4a143c3a2b8e 100644
> --- a/gdb/inferior.h
> +++ b/gdb/inferior.h
> @@ -468,7 +468,7 @@ class inferior : public refcounted_object
>    struct program_space *pspace = NULL;
>  
>    /* The arguments string to use when running.  */
> -  char *args = NULL;
> +  gdb::unique_xmalloc_ptr<char> args;
>  
>    /* The size of elements in argv.  */
>    int argc = 0;
> -- 
> 2.30.1
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] gdb: make inferior::args a unique_xmalloc_ptr
  2021-05-06 16:13 ` Andrew Burgess
@ 2021-05-06 17:17   ` Simon Marchi
  0 siblings, 0 replies; 3+ messages in thread
From: Simon Marchi @ 2021-05-06 17:17 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

On 2021-05-06 12:13 p.m., Andrew Burgess wrote:
> * Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> [2021-05-06 11:07:23 -0400]:
> 
>> Use unique_xmalloc_ptr to avoid manual memory management.
>>
>> gdb/ChangeLog:
>>
>> 	* inferior.h (class inferior) <args>: Change type to
>> 	unique_xmalloc_ptr.
>> 	* inferior.c (inferior::~inferior): Don't free args.
>> 	* infcmd.c (get_inferior_args): Adjust.
>> 	(set_inferior_args): Adjust.
> LGTM.
> 
> Thanks,
> Andre

Thanks, pushed!

Simon

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-05-06 17:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-06 15:07 [PATCH] gdb: make inferior::args a unique_xmalloc_ptr Simon Marchi
2021-05-06 16:13 ` Andrew Burgess
2021-05-06 17:17   ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).