Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [RFA 3/4] Return unique_xmalloc_ptr from gdbscm_safe_eval_string
Date: Sun, 27 May 2018 15:20:00 -0000	[thread overview]
Message-ID: <20180527152009.4228-4-tom@tromey.com> (raw)
In-Reply-To: <20180527152009.4228-1-tom@tromey.com>

This changes gdbscm_safe_eval_string to return a unique_xmalloc_ptr.
This allows for the removal of some cleanups.  It also fixes a
potential latent memory leak in gdbscm_set_backtrace.

2018-05-26  Tom Tromey  <tom@tromey.com>

	* guile/guile.c (gdbscm_eval_from_control_command): Update.
	* guile/guile-internal.h (gdbscm_safe_eval_string): Update.
	* guile/scm-objfile.c (gdbscm_execute_objfile_script): Update.
	* guile/scm-safe-call.c (gdbscm_safe_eval_string): Return
	unique_xmalloc_ptr.
---
 gdb/ChangeLog              |  8 ++++++++
 gdb/guile/guile-internal.h |  3 ++-
 gdb/guile/guile.c          | 23 +++++------------------
 gdb/guile/scm-objfile.c    | 10 +++-------
 gdb/guile/scm-safe-call.c  |  6 +++---
 5 files changed, 21 insertions(+), 29 deletions(-)

diff --git a/gdb/guile/guile-internal.h b/gdb/guile/guile-internal.h
index 20e2c70e16..2bf0cf72b2 100644
--- a/gdb/guile/guile-internal.h
+++ b/gdb/guile/guile-internal.h
@@ -402,7 +402,8 @@ extern SCM gdbscm_safe_apply_1 (SCM proc, SCM arg0, SCM args,
 
 extern SCM gdbscm_unsafe_call_1 (SCM proc, SCM arg0);
 
-extern char *gdbscm_safe_eval_string (const char *string, int display_result);
+extern gdb::unique_xmalloc_ptr<char> gdbscm_safe_eval_string
+    (const char *string, int display_result);
 
 extern char *gdbscm_safe_source_script (const char *filename);
 
diff --git a/gdb/guile/guile.c b/gdb/guile/guile.c
index 0bbbf6eac1..6b5faa38bc 100644
--- a/gdb/guile/guile.c
+++ b/gdb/guile/guile.c
@@ -197,15 +197,10 @@ guile_command (const char *arg, int from_tty)
 
   if (arg && *arg)
     {
-      char *msg = gdbscm_safe_eval_string (arg, 1);
+      gdb::unique_xmalloc_ptr<char> msg = gdbscm_safe_eval_string (arg, 1);
 
       if (msg != NULL)
-	{
-	  /* It is ok that this is a "dangling cleanup" because we
-	     throw immediately.  */
-	  make_cleanup (xfree, msg);
-	  error ("%s", msg);
-	}
+	error ("%s", msg.get ());
     }
   else
     {
@@ -253,24 +248,16 @@ static void
 gdbscm_eval_from_control_command
   (const struct extension_language_defn *extlang, struct command_line *cmd)
 {
-  char *script, *msg;
-  struct cleanup *cleanup;
+  char *script;
 
   if (cmd->body_list_1 != nullptr)
     error (_("Invalid \"guile\" block structure."));
 
-  cleanup = make_cleanup (null_cleanup, NULL);
-
   script = compute_scheme_string (cmd->body_list_0.get ());
-  msg = gdbscm_safe_eval_string (script, 0);
+  gdb::unique_xmalloc_ptr<char> msg = gdbscm_safe_eval_string (script, 0);
   xfree (script);
   if (msg != NULL)
-    {
-      make_cleanup (xfree, msg);
-      error ("%s", msg);
-    }
-
-  do_cleanups (cleanup);
+    error ("%s", msg.get ());
 }
 
 /* Read a file as Scheme code.
diff --git a/gdb/guile/scm-objfile.c b/gdb/guile/scm-objfile.c
index 9917119084..ccf7c66d33 100644
--- a/gdb/guile/scm-objfile.c
+++ b/gdb/guile/scm-objfile.c
@@ -336,16 +336,12 @@ gdbscm_execute_objfile_script (const struct extension_language_defn *extlang,
 			       struct objfile *objfile, const char *name,
 			       const char *script)
 {
-  char *msg;
-
   ofscm_current_objfile = objfile;
 
-  msg = gdbscm_safe_eval_string (script, 0 /* display_result */);
+  gdb::unique_xmalloc_ptr<char> msg
+    = gdbscm_safe_eval_string (script, 0 /* display_result */);
   if (msg != NULL)
-    {
-      fprintf_filtered (gdb_stderr, "%s", msg);
-      xfree (msg);
-    }
+    fprintf_filtered (gdb_stderr, "%s", msg.get ());
 
   ofscm_current_objfile = NULL;
 }
diff --git a/gdb/guile/scm-safe-call.c b/gdb/guile/scm-safe-call.c
index 2cba399e23..63c4833564 100644
--- a/gdb/guile/scm-safe-call.c
+++ b/gdb/guile/scm-safe-call.c
@@ -393,9 +393,9 @@ scscm_eval_scheme_string (void *datap)
    and preventing continuation capture.
    The result is NULL if no exception occurred.  Otherwise, the exception is
    printed according to "set guile print-stack" and the result is an error
-   message allocated with malloc, caller must free.  */
+   message.  */
 
-char *
+gdb::unique_xmalloc_ptr<char>
 gdbscm_safe_eval_string (const char *string, int display_result)
 {
   struct eval_scheme_string_data data = { string, display_result };
@@ -404,7 +404,7 @@ gdbscm_safe_eval_string (const char *string, int display_result)
   result = gdbscm_with_guile (scscm_eval_scheme_string, (void *) &data);
 
   if (result != NULL)
-    return xstrdup (result);
+    return gdb::unique_xmalloc_ptr<char> (xstrdup (result));
   return NULL;
 }
 \f
-- 
2.13.6


  parent reply	other threads:[~2018-05-27 15:20 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-27 15:20 [RFA 0/4] some simple cleanup removal in guile Tom Tromey
2018-05-27 15:20 ` [RFA 1/4] Use std::string in ppscm_make_pp_type_error_exception Tom Tromey
2018-07-17 13:08   ` Pedro Alves
2018-05-27 15:20 ` Tom Tromey [this message]
2018-07-17 13:08   ` [RFA 3/4] Return unique_xmalloc_ptr from gdbscm_safe_eval_string Pedro Alves
2018-05-27 15:40 ` [RFA 2/4] Change gdbscm_exception_message_to_string to return a unique_xmalloc_ptr Tom Tromey
2018-07-17 13:08   ` Pedro Alves
2018-07-17 19:11     ` Tom Tromey
2018-07-18 13:29       ` [RFC] gdbscm_wrap, eliminate GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (Re: [RFA 2/4] Change gdbscm_exception_message_to_string to return a unique_xmalloc_ptr) Pedro Alves
2018-07-18 19:34         ` Tom Tromey
2018-07-18 22:31           ` Pedro Alves
2018-05-27 15:54 ` [RFA 4/4] Return unique_xmalloc_ptr from gdbscm_scm_to_string Tom Tromey
2018-07-17 13:09   ` Pedro Alves
2018-06-18 14:37 ` [RFA 0/4] some simple cleanup removal in guile Tom Tromey
2018-07-16 16:35   ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180527152009.4228-4-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox