From patchwork Fri Aug 18 20:00:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 22230 Received: (qmail 98422 invoked by alias); 18 Aug 2017 20:01:08 -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 88957 invoked by uid 89); 18 Aug 2017 20:00:33 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=Hx-spam-relays-external:10.0.90.82, H*RU:10.0.90.82, H*r:CMOut01, H*RU:CMOut01 X-HELO: gproxy6-pub.mail.unifiedlayer.com Received: from gproxy6-pub.mail.unifiedlayer.com (HELO gproxy6-pub.mail.unifiedlayer.com) (67.222.39.168) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 18 Aug 2017 20:00:31 +0000 Received: from CMOut01 (unknown [10.0.90.82]) by gproxy6.mail.unifiedlayer.com (Postfix) with ESMTP id BEF5A1E08B0 for ; Fri, 18 Aug 2017 14:00:29 -0600 (MDT) Received: from box522.bluehost.com ([74.220.219.122]) by CMOut01 with id yk0S1v00p2f2jeq01k0Vok; Fri, 18 Aug 2017 14:00:29 -0600 X-Authority-Analysis: v=2.2 cv=FrR1xyjq c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=KeKAF7QvOSUA:10 a=zstS-IiYAAAA:8 a=20KFwNOVAAAA:8 a=nXodW-eaXK8yhudpei8A:9 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 75-166-24-97.hlrn.qwest.net ([75.166.24.97]:49122 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1dinRS-001KNP-6o; Fri, 18 Aug 2017 14:00:26 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 1/5] Change gdb_abspath to return a unique_xmalloc_ptr Date: Fri, 18 Aug 2017 14:00:20 -0600 Message-Id: <20170818200024.4948-2-tom@tromey.com> In-Reply-To: <20170818200024.4948-1-tom@tromey.com> References: <20170818200024.4948-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1dinRS-001KNP-6o X-Source-Sender: 75-166-24-97.hlrn.qwest.net (bapiya.Home) [75.166.24.97]:49122 X-Source-Auth: tom+tromey.com X-Email-Count: 2 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-Local-Domain: yes This changes gdb_abspath to return a unique_xmalloc_ptr, and fixes up the callers. This allows the removal of a cleanup, and also puts ownership rules into the API, where they belong. ChangeLog 2017-08-18 Tom Tromey * compile/compile.c (compile_file_command): Use gdb::unique_xmalloc_ptr, std::string. * utils.c (gdb_abspath): Change return type. * source.c (openp): Update. * objfiles.c (allocate_objfile): Update. * main.c (set_gdb_data_directory): Update. * utils.h (gdb_abspath): Return a gdb::unique_xmalloc_ptr. --- gdb/ChangeLog | 10 ++++++++++ gdb/compile/compile.c | 11 +++-------- gdb/main.c | 4 ++-- gdb/objfiles.c | 11 +++++++---- gdb/source.c | 2 +- gdb/utils.c | 21 +++++++++------------ gdb/utils.h | 2 +- 7 files changed, 33 insertions(+), 28 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 7ad3f4c..e75d1b7 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,4 +1,14 @@ 2017-08-18 Tom Tromey + + * compile/compile.c (compile_file_command): Use + gdb::unique_xmalloc_ptr, std::string. + * utils.c (gdb_abspath): Change return type. + * source.c (openp): Update. + * objfiles.c (allocate_objfile): Update. + * main.c (set_gdb_data_directory): Update. + * utils.h (gdb_abspath): Return a gdb::unique_xmalloc_ptr. + +2017-08-18 Tom Tromey Pedro Alves * spu-multiarch.c (parse_spufs_run): Use scoped_restore. diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c index bca7b57..91e084f 100644 --- a/gdb/compile/compile.c +++ b/gdb/compile/compile.c @@ -90,8 +90,6 @@ static void compile_file_command (char *arg, int from_tty) { enum compile_i_scope_types scope = COMPILE_I_SIMPLE_SCOPE; - char *buffer; - struct cleanup *cleanup; scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0); @@ -115,12 +113,9 @@ compile_file_command (char *arg, int from_tty) error (_("Unknown argument specified.")); arg = skip_spaces (arg); - arg = gdb_abspath (arg); - cleanup = make_cleanup (xfree, arg); - buffer = xstrprintf ("#include \"%s\"\n", arg); - make_cleanup (xfree, buffer); - eval_compile_command (NULL, buffer, scope, NULL); - do_cleanups (cleanup); + gdb::unique_xmalloc_ptr abspath = gdb_abspath (arg); + std::string buffer = string_printf ("#include \"%s\"\n", abspath.get ()); + eval_compile_command (NULL, buffer.c_str (), scope, NULL); } /* Handle the input from the 'compile code' command. The diff --git a/gdb/main.c b/gdb/main.c index 9813041..886e17f 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -128,10 +128,10 @@ set_gdb_data_directory (const char *new_datadir) isn't canonical, but that's ok. */ if (!IS_ABSOLUTE_PATH (gdb_datadir)) { - char *abs_datadir = gdb_abspath (gdb_datadir); + gdb::unique_xmalloc_ptr abs_datadir = gdb_abspath (gdb_datadir); xfree (gdb_datadir); - gdb_datadir = abs_datadir; + gdb_datadir = abs_datadir.release (); } } diff --git a/gdb/objfiles.c b/gdb/objfiles.c index d261c87..ff99ca6 100644 --- a/gdb/objfiles.c +++ b/gdb/objfiles.c @@ -386,22 +386,25 @@ allocate_objfile (bfd *abfd, const char *name, objfile_flags flags) objfile_alloc_data (objfile); + gdb::unique_xmalloc_ptr name_holder; if (name == NULL) { gdb_assert (abfd == NULL); gdb_assert ((flags & OBJF_NOT_FILENAME) != 0); - expanded_name = xstrdup ("<>"); + expanded_name = "<>"; } else if ((flags & OBJF_NOT_FILENAME) != 0 || is_target_filename (name)) - expanded_name = xstrdup (name); + expanded_name = name; else - expanded_name = gdb_abspath (name); + { + name_holder = gdb_abspath (name); + expanded_name = name_holder.get (); + } objfile->original_name = (char *) obstack_copy0 (&objfile->objfile_obstack, expanded_name, strlen (expanded_name)); - xfree (expanded_name); /* Update the per-objfile information that comes from the bfd, ensuring that any data that is reference is saved in the per-objfile data diff --git a/gdb/source.c b/gdb/source.c index 769d9ef..e2a507d 100644 --- a/gdb/source.c +++ b/gdb/source.c @@ -913,7 +913,7 @@ done: else if ((opts & OPF_RETURN_REALPATH) != 0) *filename_opened = gdb_realpath (filename); else - *filename_opened = gdb_abspath (filename); + *filename_opened = gdb_abspath (filename).release (); } errno = last_errno; diff --git a/gdb/utils.c b/gdb/utils.c index 96ae709..9959c01 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -2761,28 +2761,25 @@ gdb_realpath_keepfile (const char *filename) /* Return PATH in absolute form, performing tilde-expansion if necessary. PATH cannot be NULL or the empty string. - This does not resolve symlinks however, use gdb_realpath for that. - Space for the result is allocated with malloc. - If the path is already absolute, it is strdup'd. - If there is a problem computing the absolute path, the path is returned - unchanged (still strdup'd). */ + This does not resolve symlinks however, use gdb_realpath for that. */ -char * +gdb::unique_xmalloc_ptr gdb_abspath (const char *path) { gdb_assert (path != NULL && path[0] != '\0'); if (path[0] == '~') - return tilde_expand (path); + return gdb::unique_xmalloc_ptr (tilde_expand (path)); if (IS_ABSOLUTE_PATH (path)) - return xstrdup (path); + return gdb::unique_xmalloc_ptr (xstrdup (path)); /* Beware the // my son, the Emacs barfs, the botch that catch... */ - return concat (current_directory, - IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1]) - ? "" : SLASH_STRING, - path, (char *) NULL); + return gdb::unique_xmalloc_ptr + (concat (current_directory, + IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1]) + ? "" : SLASH_STRING, + path, (char *) NULL)); } ULONGEST diff --git a/gdb/utils.h b/gdb/utils.h index bb5fadc..a2a959f 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -256,7 +256,7 @@ extern char *gdb_realpath (const char *); extern char *gdb_realpath_keepfile (const char *); -extern char *gdb_abspath (const char *); +extern gdb::unique_xmalloc_ptr gdb_abspath (const char *); extern int gdb_filename_fnmatch (const char *pattern, const char *string, int flags);