From e3e41d588adbe26a6ca54338dd4915382d981a3e Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Thu, 3 Aug 2017 16:32:14 -0600 Subject: Change gdb_abspath to return a unique_xmalloc_ptr 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-22 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 0908b99..bb49bc8 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,13 @@ +2017-08-22 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-22 Zhouyi Zhou * cli-cmds.c (list_commands): List actual code around more than 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); -- cgit v1.1