From patchwork Sun May 27 15:20:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 27520 Received: (qmail 59672 invoked by alias); 27 May 2018 15:20:17 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 59492 invoked by uid 89); 27 May 2018 15:20:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=preventing X-HELO: gateway23.websitewelcome.com Received: from gateway23.websitewelcome.com (HELO gateway23.websitewelcome.com) (192.185.49.184) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 27 May 2018 15:20:15 +0000 Received: from cm15.websitewelcome.com (cm15.websitewelcome.com [100.42.49.9]) by gateway23.websitewelcome.com (Postfix) with ESMTP id AE8572E7A for ; Sun, 27 May 2018 10:20:13 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id MxSvfJHZebXuJMxSvfa9zr; Sun, 27 May 2018 10:20:13 -0500 X-Authority-Reason: nr=8 Received: from 174-29-44-154.hlrn.qwest.net ([174.29.44.154]:51012 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1fMxSv-004Ahx-En; Sun, 27 May 2018 10:20:13 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 3/4] Return unique_xmalloc_ptr from gdbscm_safe_eval_string Date: Sun, 27 May 2018 09:20:08 -0600 Message-Id: <20180527152009.4228-4-tom@tromey.com> In-Reply-To: <20180527152009.4228-1-tom@tromey.com> References: <20180527152009.4228-1-tom@tromey.com> X-BWhitelist: no X-Source-L: No X-Exim-ID: 1fMxSv-004Ahx-En X-Source-Sender: 174-29-44-154.hlrn.qwest.net (bapiya.Home) [174.29.44.154]:51012 X-Source-Auth: tom+tromey.com X-Email-Count: 4 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes 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 * 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 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 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 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 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 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 (xstrdup (result)); return NULL; }