public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [RFA 1/5] Change gdb_abspath to return a unique_xmalloc_ptr
  2017-08-18 20:01 [RFA 0/5] simple string changes Tom Tromey
                   ` (3 preceding siblings ...)
  2017-08-18 20:01 ` [RFA 5/5] Change psymtab_search_name " Tom Tromey
@ 2017-08-18 20:01 ` Tom Tromey
  2017-08-21  9:57 ` [RFA 0/5] simple string changes Pedro Alves
  5 siblings, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2017-08-18 20:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

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  <tom@tromey.com>

	* 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  <tom@tromey.com>
+
+	* 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  <tom@tromey.com>
 	    Pedro Alves  <palves@redhat.com>
 
 	* 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 (&current_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<char> 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<char> 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<char> name_holder;
   if (name == NULL)
     {
       gdb_assert (abfd == NULL);
       gdb_assert ((flags & OBJF_NOT_FILENAME) != 0);
-      expanded_name = xstrdup ("<<anonymous objfile>>");
+      expanded_name = "<<anonymous objfile>>";
     }
   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<char>
 gdb_abspath (const char *path)
 {
   gdb_assert (path != NULL && path[0] != '\0');
 
   if (path[0] == '~')
-    return tilde_expand (path);
+    return gdb::unique_xmalloc_ptr<char> (tilde_expand (path));
 
   if (IS_ABSOLUTE_PATH (path))
-    return xstrdup (path);
+    return gdb::unique_xmalloc_ptr<char> (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<char>
+    (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<char> gdb_abspath (const char *);
 
 extern int gdb_filename_fnmatch (const char *pattern, const char *string,
 				 int flags);
-- 
2.9.4

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

* [RFA 2/5] Change gdb_realpath_keepfile to return a unique_xmalloc_ptr
  2017-08-18 20:01 [RFA 0/5] simple string changes Tom Tromey
  2017-08-18 20:01 ` [RFA 3/5] Change gdb_realpath to return a unique_xmalloc_ptr Tom Tromey
@ 2017-08-18 20:01 ` Tom Tromey
  2017-08-18 20:01 ` [RFA 4/5] Change rewrite_source_path " Tom Tromey
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2017-08-18 20:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes gdb_realpath_keepfile to return a unique_xmalloc_ptr, and
fixes up the callers.

ChangeLog
2017-08-18  Tom Tromey  <tom@tromey.com>

	* utils.c (gdb_realpath_keepfile): Return a
	gdb::unique_xmalloc_ptr.
	* exec.c (exec_file_attach): Update.
	* utils.h (gdb_realpath_keepfile): Return a
	gdb::unique_xmalloc_ptr.
---
 gdb/ChangeLog | 8 ++++++++
 gdb/exec.c    | 2 +-
 gdb/utils.c   | 6 +++---
 gdb/utils.h   | 2 +-
 4 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index e75d1b7..ec022c3 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,13 @@
 2017-08-18  Tom Tromey  <tom@tromey.com>
 
+	* utils.c (gdb_realpath_keepfile): Return a
+	gdb::unique_xmalloc_ptr.
+	* exec.c (exec_file_attach): Update.
+	* utils.h (gdb_realpath_keepfile): Return a
+	gdb::unique_xmalloc_ptr.
+
+2017-08-18  Tom Tromey  <tom@tromey.com>
+
 	* compile/compile.c (compile_file_command): Use
 	gdb::unique_xmalloc_ptr, std::string.
 	* utils.c (gdb_abspath): Change return type.
diff --git a/gdb/exec.c b/gdb/exec.c
index 6980b07..45bc133 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -352,7 +352,7 @@ exec_file_attach (const char *filename, int from_tty)
       if (load_via_target)
 	exec_filename = xstrdup (bfd_get_filename (exec_bfd));
       else
-	exec_filename = gdb_realpath_keepfile (scratch_pathname);
+	exec_filename = gdb_realpath_keepfile (scratch_pathname).release ();
 
       if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
 	{
diff --git a/gdb/utils.c b/gdb/utils.c
index 9959c01..5bdc638 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -2716,7 +2716,7 @@ gdb_realpath (const char *filename)
 /* Return a copy of FILENAME, with its directory prefix canonicalized
    by gdb_realpath.  */
 
-char *
+gdb::unique_xmalloc_ptr<char>
 gdb_realpath_keepfile (const char *filename)
 {
   const char *base_name = lbasename (filename);
@@ -2727,7 +2727,7 @@ gdb_realpath_keepfile (const char *filename)
   /* Extract the basename of filename, and return immediately 
      a copy of filename if it does not contain any directory prefix.  */
   if (base_name == filename)
-    return xstrdup (filename);
+    return gdb::unique_xmalloc_ptr<char> (xstrdup (filename));
 
   dir_name = (char *) alloca ((size_t) (base_name - filename + 2));
   /* Allocate enough space to store the dir_name + plus one extra
@@ -2756,7 +2756,7 @@ gdb_realpath_keepfile (const char *filename)
     result = concat (real_path, SLASH_STRING, base_name, (char *) NULL);
 
   xfree (real_path);
-  return result;
+  return gdb::unique_xmalloc_ptr<char> (result);
 }
 
 /* Return PATH in absolute form, performing tilde-expansion if necessary.
diff --git a/gdb/utils.h b/gdb/utils.h
index a2a959f..477257b 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -254,7 +254,7 @@ extern struct cleanup *make_bpstat_clear_actions_cleanup (void);
 
 extern char *gdb_realpath (const char *);
 
-extern char *gdb_realpath_keepfile (const char *);
+extern gdb::unique_xmalloc_ptr<char> gdb_realpath_keepfile (const char *);
 
 extern gdb::unique_xmalloc_ptr<char> gdb_abspath (const char *);
 
-- 
2.9.4

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

* [RFA 5/5] Change psymtab_search_name to return a unique_xmalloc_ptr
  2017-08-18 20:01 [RFA 0/5] simple string changes Tom Tromey
                   ` (2 preceding siblings ...)
  2017-08-18 20:01 ` [RFA 4/5] Change rewrite_source_path " Tom Tromey
@ 2017-08-18 20:01 ` Tom Tromey
  2017-08-18 20:01 ` [RFA 1/5] Change gdb_abspath " Tom Tromey
  2017-08-21  9:57 ` [RFA 0/5] simple string changes Pedro Alves
  5 siblings, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2017-08-18 20:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes psymtab_search_name to return a unique_xmalloc_ptr and
fixes up its one caller.  This allows the removal of some cleanups.

ChangeLog
2017-08-18  Tom Tromey  <tom@tromey.com>

	* psymtab.c (psymtab_search_name): Return a unique_xmalloc_ptr.
	(lookup_partial_symbol): Update.
---
 gdb/ChangeLog |  5 +++++
 gdb/psymtab.c | 32 ++++++++++++--------------------
 2 files changed, 17 insertions(+), 20 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index a15df78..4e03f29 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,10 @@
 2017-08-18  Tom Tromey  <tom@tromey.com>
 
+	* psymtab.c (psymtab_search_name): Return a unique_xmalloc_ptr.
+	(lookup_partial_symbol): Update.
+
+2017-08-18  Tom Tromey  <tom@tromey.com>
+
 	* source.h (rewrite_source_path): Return a unique_xmalloc_ptr.
 	* source.c (rewrite_source_path): Return a unique_xmalloc_ptr.
 	(find_and_open_source, symtab_to_fullname): Update.
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index 6307d6e..4cbf91c 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -627,7 +627,7 @@ match_partial_symbol (struct objfile *objfile,
 
    The caller is responsible for freeing the return result.  */
 
-static char *
+static gdb::unique_xmalloc_ptr<char>
 psymtab_search_name (const char *name)
 {
   switch (current_language->la_language)
@@ -639,7 +639,7 @@ psymtab_search_name (const char *name)
 	    char *ret = cp_remove_params (name);
 
 	    if (ret)
-	      return ret;
+	      return gdb::unique_xmalloc_ptr<char> (ret);
 	  }
       }
       break;
@@ -648,7 +648,7 @@ psymtab_search_name (const char *name)
       break;
     }
 
-  return xstrdup (name);
+  return gdb::unique_xmalloc_ptr<char> (xstrdup (name));
 }
 
 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
@@ -663,14 +663,11 @@ lookup_partial_symbol (struct objfile *objfile,
   struct partial_symbol **top, **real_top, **bottom, **center;
   int length = (global ? pst->n_global_syms : pst->n_static_syms);
   int do_linear_search = 1;
-  char *search_name;
-  struct cleanup *cleanup;
 
   if (length == 0)
     return NULL;
 
-  search_name = psymtab_search_name (name);
-  cleanup = make_cleanup (xfree, search_name);
+  gdb::unique_xmalloc_ptr<char> search_name = psymtab_search_name (name);
   start = (global ?
 	   objfile->global_psymbols.list + pst->globals_offset :
 	   objfile->static_psymbols.list + pst->statics_offset);
@@ -695,7 +692,7 @@ lookup_partial_symbol (struct objfile *objfile,
 	    internal_error (__FILE__, __LINE__,
 			    _("failed internal consistency check"));
 	  if (strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*center),
-				 search_name) >= 0)
+				 search_name.get ()) >= 0)
 	    {
 	      top = center;
 	    }
@@ -710,20 +707,19 @@ lookup_partial_symbol (struct objfile *objfile,
 
       /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
 	 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME.  */
-      while (top >= start && SYMBOL_MATCHES_SEARCH_NAME (*top, search_name))
+      while (top >= start && SYMBOL_MATCHES_SEARCH_NAME (*top,
+							 search_name.get ()))
 	top--;
 
       /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME.  */
       top++;
 
-      while (top <= real_top && SYMBOL_MATCHES_SEARCH_NAME (*top, search_name))
+      while (top <= real_top
+	     && SYMBOL_MATCHES_SEARCH_NAME (*top, search_name.get ()))
 	{
 	  if (symbol_matches_domain (SYMBOL_LANGUAGE (*top),
 				     SYMBOL_DOMAIN (*top), domain))
-	    {
-	      do_cleanups (cleanup);
-	      return *top;
-	    }
+	    return *top;
 	  top++;
 	}
     }
@@ -737,15 +733,11 @@ lookup_partial_symbol (struct objfile *objfile,
 	{
 	  if (symbol_matches_domain (SYMBOL_LANGUAGE (*psym),
 				     SYMBOL_DOMAIN (*psym), domain)
-	      && SYMBOL_MATCHES_SEARCH_NAME (*psym, search_name))
-	    {
-	      do_cleanups (cleanup);
-	      return *psym;
-	    }
+	      && SYMBOL_MATCHES_SEARCH_NAME (*psym, search_name.get ()))
+	    return *psym;
 	}
     }
 
-  do_cleanups (cleanup);
   return NULL;
 }
 
-- 
2.9.4

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

* [RFA 4/5] Change rewrite_source_path to return a unique_xmalloc_ptr
  2017-08-18 20:01 [RFA 0/5] simple string changes Tom Tromey
  2017-08-18 20:01 ` [RFA 3/5] Change gdb_realpath to return a unique_xmalloc_ptr Tom Tromey
  2017-08-18 20:01 ` [RFA 2/5] Change gdb_realpath_keepfile " Tom Tromey
@ 2017-08-18 20:01 ` Tom Tromey
  2017-08-18 20:01 ` [RFA 5/5] Change psymtab_search_name " Tom Tromey
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2017-08-18 20:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes rewrite_source_path to return a unique_xmalloc_ptr and
fixes up the callers.  This allows removing some cleanups.

ChangeLog
2017-08-18  Tom Tromey  <tom@tromey.com>

	* source.h (rewrite_source_path): Return a unique_xmalloc_ptr.
	* source.c (rewrite_source_path): Return a unique_xmalloc_ptr.
	(find_and_open_source, symtab_to_fullname): Update.
	* psymtab.c (psymtab_to_fullname): Update.
---
 gdb/ChangeLog |  7 +++++++
 gdb/psymtab.c | 15 ++++++---------
 gdb/source.c  | 50 +++++++++++++++++++-------------------------------
 gdb/source.h  |  2 +-
 4 files changed, 33 insertions(+), 41 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 0257189..a15df78 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,12 @@
 2017-08-18  Tom Tromey  <tom@tromey.com>
 
+	* source.h (rewrite_source_path): Return a unique_xmalloc_ptr.
+	* source.c (rewrite_source_path): Return a unique_xmalloc_ptr.
+	(find_and_open_source, symtab_to_fullname): Update.
+	* psymtab.c (psymtab_to_fullname): Update.
+
+2017-08-18  Tom Tromey  <tom@tromey.com>
+
 	* exec.c (exec_file_attach): Update.
 	* linux-thread-db.c (try_thread_db_load): Update.
 	* guile/scm-safe-call.c (gdbscm_safe_source_script): Update.
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index 8283545..6307d6e 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -1199,23 +1199,20 @@ psymtab_to_fullname (struct partial_symtab *ps)
 	close (fd);
       else
 	{
-	  char *fullname;
-	  struct cleanup *back_to;
+	  gdb::unique_xmalloc_ptr<char> fullname;
 
 	  /* rewrite_source_path would be applied by find_and_open_source, we
 	     should report the pathname where GDB tried to find the file.  */
 
 	  if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
-	    fullname = xstrdup (ps->filename);
+	    fullname.reset (xstrdup (ps->filename));
 	  else
-	    fullname = concat (ps->dirname, SLASH_STRING,
-			       ps->filename, (char *) NULL);
+	    fullname.reset (concat (ps->dirname, SLASH_STRING,
+				    ps->filename, (char *) NULL));
 
-	  back_to = make_cleanup (xfree, fullname);
-	  ps->fullname = rewrite_source_path (fullname);
+	  ps->fullname = rewrite_source_path (fullname.get ()).release ();
 	  if (ps->fullname == NULL)
-	    ps->fullname = xstrdup (fullname);
-	  do_cleanups (back_to);
+	    ps->fullname = fullname.release ();
 	}
     }
 
diff --git a/gdb/source.c b/gdb/source.c
index 0453f92..5403715 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -994,13 +994,12 @@ get_substitute_path_rule (const char *path)
 }
 
 /* If the user specified a source path substitution rule that applies
-   to PATH, then apply it and return the new path.  This new path must
-   be deallocated afterwards.
-   
+   to PATH, then apply it and return the new path.
+
    Return NULL if no substitution rule was specified by the user,
    or if no rule applied to the given PATH.  */
-   
-char *
+
+gdb::unique_xmalloc_ptr<char>
 rewrite_source_path (const char *path)
 {
   const struct substitute_path_rule *rule = get_substitute_path_rule (path);
@@ -1019,7 +1018,7 @@ rewrite_source_path (const char *path)
   strcpy (new_path, rule->to);
   strcat (new_path, path + from_len);
 
-  return new_path;
+  return gdb::unique_xmalloc_ptr<char> (new_path);
 }
 
 int
@@ -1030,7 +1029,6 @@ find_and_open_source (const char *filename,
   char *path = source_path;
   const char *p;
   int result;
-  struct cleanup *cleanup;
 
   /* Quick way out if we already know its full name.  */
 
@@ -1039,7 +1037,7 @@ find_and_open_source (const char *filename,
       /* The user may have requested that source paths be rewritten
          according to substitution rules he provided.  If a substitution
          rule applies to this path, then apply it.  */
-      char *rewritten_fullname = rewrite_source_path (*fullname);
+      char *rewritten_fullname = rewrite_source_path (*fullname).release ();
 
       if (rewritten_fullname != NULL)
         {
@@ -1062,21 +1060,17 @@ find_and_open_source (const char *filename,
       *fullname = NULL;
     }
 
-  cleanup = make_cleanup (null_cleanup, NULL);
-
+  gdb::unique_xmalloc_ptr<char> rewritten_dirname;
   if (dirname != NULL)
     {
       /* If necessary, rewrite the compilation directory name according
          to the source path substitution rules specified by the user.  */
 
-      char *rewritten_dirname = rewrite_source_path (dirname);
+      rewritten_dirname = rewrite_source_path (dirname);
 
       if (rewritten_dirname != NULL)
-        {
-          make_cleanup (xfree, rewritten_dirname);
-          dirname = rewritten_dirname;
-        }
-      
+	dirname = rewritten_dirname.get ();
+
       /* Replace a path entry of $cdir with the compilation directory
 	 name.  */
 #define	cdir_len	5
@@ -1098,17 +1092,15 @@ find_and_open_source (const char *filename,
 	}
     }
 
+  gdb::unique_xmalloc_ptr<char> rewritten_filename;
   if (IS_ABSOLUTE_PATH (filename))
     {
       /* If filename is absolute path, try the source path
 	 substitution on it.  */
-      char *rewritten_filename = rewrite_source_path (filename);
+      rewritten_filename = rewrite_source_path (filename);
 
       if (rewritten_filename != NULL)
-        {
-          make_cleanup (xfree, rewritten_filename);
-          filename = rewritten_filename;
-        }
+	filename = rewritten_filename.get ();
     }
 
   result = openp (path, OPF_SEARCH_IN_PATH | OPF_RETURN_REALPATH, filename,
@@ -1122,7 +1114,6 @@ find_and_open_source (const char *filename,
 			OPEN_MODE, fullname);
     }
 
-  do_cleanups (cleanup);
   return result;
 }
 
@@ -1164,23 +1155,20 @@ symtab_to_fullname (struct symtab *s)
 	close (fd);
       else
 	{
-	  char *fullname;
-	  struct cleanup *back_to;
+	  gdb::unique_xmalloc_ptr<char> fullname;
 
 	  /* rewrite_source_path would be applied by find_and_open_source, we
 	     should report the pathname where GDB tried to find the file.  */
 
 	  if (SYMTAB_DIRNAME (s) == NULL || IS_ABSOLUTE_PATH (s->filename))
-	    fullname = xstrdup (s->filename);
+	    fullname.reset (xstrdup (s->filename));
 	  else
-	    fullname = concat (SYMTAB_DIRNAME (s), SLASH_STRING,
-			       s->filename, (char *) NULL);
+	    fullname.reset (concat (SYMTAB_DIRNAME (s), SLASH_STRING,
+				    s->filename, (char *) NULL));
 
-	  back_to = make_cleanup (xfree, fullname);
-	  s->fullname = rewrite_source_path (fullname);
+	  s->fullname = rewrite_source_path (fullname.get ()).release ();
 	  if (s->fullname == NULL)
-	    s->fullname = xstrdup (fullname);
-	  do_cleanups (back_to);
+	    s->fullname = fullname.release ();
 	}
     } 
 
diff --git a/gdb/source.h b/gdb/source.h
index ee9f3fa..263715a 100644
--- a/gdb/source.h
+++ b/gdb/source.h
@@ -48,7 +48,7 @@ extern int find_and_open_source (const char *filename,
    negative number for error.  */
 extern int open_source_file (struct symtab *s);
 
-extern char *rewrite_source_path (const char *path);
+extern gdb::unique_xmalloc_ptr<char> rewrite_source_path (const char *path);
 
 extern const char *symtab_to_fullname (struct symtab *s);
 
-- 
2.9.4

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

* [RFA 0/5] simple string changes
@ 2017-08-18 20:01 Tom Tromey
  2017-08-18 20:01 ` [RFA 3/5] Change gdb_realpath to return a unique_xmalloc_ptr Tom Tromey
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Tom Tromey @ 2017-08-18 20:01 UTC (permalink / raw)
  To: gdb-patches

I had sent this patch a little while ago, but it was lost in the
sourceware crash.

This series fixes a few spots doing manual management of a char* to
use gdb::unique_xmalloc_ptr or std::string, as appropriate.

Regression tested on the buildbot.

Tom

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

* [RFA 3/5] Change gdb_realpath to return a unique_xmalloc_ptr
  2017-08-18 20:01 [RFA 0/5] simple string changes Tom Tromey
@ 2017-08-18 20:01 ` Tom Tromey
  2017-08-18 20:01 ` [RFA 2/5] Change gdb_realpath_keepfile " Tom Tromey
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2017-08-18 20:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes gdb_realpath to return a unique_xmalloc_ptr and fixes up
the callers.  This allows removing some cleanups.  This change by
itself caused xfullpath.exp to fail; and attempting to fix that ran
into various problems (like .get() being optimized out); so this patch
also rewrites xfullpath.exp to be a C++ selftest instead.

ChangeLog
2017-08-18  Tom Tromey  <tom@tromey.com>

	* exec.c (exec_file_attach): Update.
	* linux-thread-db.c (try_thread_db_load): Update.
	* guile/scm-safe-call.c (gdbscm_safe_source_script): Update.
	* utils.c (gdb_realpath): Change return type.
	(gdb_realpath_keepfile): Update.
	(gdb_realpath_check_trailer, gdb_realpath_tests): New functions.
	(_initialize_utils): Register the new self test.
	* source.c (openp): Update.
	(find_and_open_source): Update.
	* nto-tdep.c (nto_find_and_open_solib): Update.
	* main.c (set_gdb_data_directory): Update.
	(captured_main_1): Update.
	* dwarf2read.c (dwarf2_get_dwz_file): Update
	(dw2_map_symbol_filenames): Update.
	* auto-load.c (auto_load_safe_path_vec_update): Update.
	(filename_is_in_auto_load_safe_path_vec): Change type of
	"filename_realp".
	(auto_load_objfile_script): Update.
	(file_is_auto_load_safe): Update.  Use std::string.
	* utils.h (gdb_realpath): Return a gdb::unique_xmalloc_ptr.

testsuite/ChangeLog
2017-08-18  Tom Tromey  <tom@tromey.com>

	* gdb.gdb/xfullpath.exp: Remove.
---
 gdb/ChangeLog                       | 23 +++++++++++++
 gdb/auto-load.c                     | 69 +++++++++++++++----------------------
 gdb/dwarf2read.c                    | 14 ++++----
 gdb/exec.c                          | 27 ++++++---------
 gdb/guile/scm-safe-call.c           |  5 ++-
 gdb/linux-thread-db.c               |  2 +-
 gdb/main.c                          |  5 +--
 gdb/nto-tdep.c                      |  2 +-
 gdb/objfiles.c                      |  2 +-
 gdb/source.c                        |  4 +--
 gdb/symtab.c                        |  2 +-
 gdb/testsuite/ChangeLog             |  4 +++
 gdb/testsuite/gdb.gdb/xfullpath.exp | 60 --------------------------------
 gdb/utils.c                         | 54 +++++++++++++++++++++++++----
 gdb/utils.h                         |  2 +-
 15 files changed, 130 insertions(+), 145 deletions(-)
 delete mode 100644 gdb/testsuite/gdb.gdb/xfullpath.exp

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index ec022c3..0257189 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,28 @@
 2017-08-18  Tom Tromey  <tom@tromey.com>
 
+	* exec.c (exec_file_attach): Update.
+	* linux-thread-db.c (try_thread_db_load): Update.
+	* guile/scm-safe-call.c (gdbscm_safe_source_script): Update.
+	* utils.c (gdb_realpath): Change return type.
+	(gdb_realpath_keepfile): Update.
+	(gdb_realpath_check_trailer, gdb_realpath_tests): New functions.
+	(_initialize_utils): Register the new self test.
+	* source.c (openp): Update.
+	(find_and_open_source): Update.
+	* nto-tdep.c (nto_find_and_open_solib): Update.
+	* main.c (set_gdb_data_directory): Update.
+	(captured_main_1): Update.
+	* dwarf2read.c (dwarf2_get_dwz_file): Update
+	(dw2_map_symbol_filenames): Update.
+	* auto-load.c (auto_load_safe_path_vec_update): Update.
+	(filename_is_in_auto_load_safe_path_vec): Change type of
+	"filename_realp".
+	(auto_load_objfile_script): Update.
+	(file_is_auto_load_safe): Update.  Use std::string.
+	* utils.h (gdb_realpath): Return a gdb::unique_xmalloc_ptr.
+
+2017-08-18  Tom Tromey  <tom@tromey.com>
+
 	* utils.c (gdb_realpath_keepfile): Return a
 	gdb::unique_xmalloc_ptr.
 	* exec.c (exec_file_attach): Update.
diff --git a/gdb/auto-load.c b/gdb/auto-load.c
index 9f1f13f..5bba813 100644
--- a/gdb/auto-load.c
+++ b/gdb/auto-load.c
@@ -218,7 +218,7 @@ auto_load_safe_path_vec_update (void)
     {
       char *dir = VEC_index (char_ptr, auto_load_safe_path_vec, ix);
       char *expanded = tilde_expand (dir);
-      char *real_path = gdb_realpath (expanded);
+      gdb::unique_xmalloc_ptr<char> real_path = gdb_realpath (expanded);
 
       /* Ensure the current entry is at least tilde_expand-ed.  */
       VEC_replace (char_ptr, auto_load_safe_path_vec, ix, expanded);
@@ -238,16 +238,15 @@ auto_load_safe_path_vec_update (void)
       xfree (dir);
 
       /* If gdb_realpath returns a different content, append it.  */
-      if (strcmp (real_path, expanded) == 0)
-	xfree (real_path);
-      else
+      if (strcmp (real_path.get (), expanded) != 0)
 	{
-	  VEC_safe_push (char_ptr, auto_load_safe_path_vec, real_path);
-
 	  if (debug_auto_load)
 	    fprintf_unfiltered (gdb_stdlog,
 				_("auto-load: And canonicalized as \"%s\".\n"),
-				real_path);
+				real_path.get ());
+
+	  VEC_safe_push (char_ptr, auto_load_safe_path_vec,
+			 real_path.release ());
 	}
     }
 }
@@ -419,12 +418,11 @@ filename_is_in_pattern (const char *filename, const char *pattern)
 /* Return 1 if FILENAME belongs to one of directory components of
    AUTO_LOAD_SAFE_PATH_VEC.  Return 0 otherwise.
    auto_load_safe_path_vec_update is never called.
-   *FILENAME_REALP may be updated by gdb_realpath of FILENAME - it has to be
-   freed by the caller.  */
+   *FILENAME_REALP may be updated by gdb_realpath of FILENAME.  */
 
 static int
 filename_is_in_auto_load_safe_path_vec (const char *filename,
-					char **filename_realp)
+					gdb::unique_xmalloc_ptr<char> *filename_realp)
 {
   char *pattern;
   int ix;
@@ -439,17 +437,17 @@ filename_is_in_auto_load_safe_path_vec (const char *filename,
       if (*filename_realp == NULL)
 	{
 	  *filename_realp = gdb_realpath (filename);
-	  if (debug_auto_load && strcmp (*filename_realp, filename) != 0)
+	  if (debug_auto_load && strcmp (filename_realp->get (), filename) != 0)
 	    fprintf_unfiltered (gdb_stdlog,
 				_("auto-load: Resolved "
 				  "file \"%s\" as \"%s\".\n"),
-				filename, *filename_realp);
+				filename, filename_realp->get ());
 	}
 
-      if (strcmp (*filename_realp, filename) != 0)
+      if (strcmp (filename_realp->get (), filename) != 0)
 	for (ix = 0;
 	     VEC_iterate (char_ptr, auto_load_safe_path_vec, ix, pattern); ++ix)
-	  if (filename_is_in_pattern (*filename_realp, pattern))
+	  if (filename_is_in_pattern (filename_realp->get (), pattern))
 	    break;
     }
 
@@ -476,8 +474,7 @@ filename_is_in_auto_load_safe_path_vec (const char *filename,
 int
 file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
 {
-  char *filename_real = NULL;
-  struct cleanup *back_to;
+  gdb::unique_xmalloc_ptr<char> filename_real;
   static int advice_printed = 0;
 
   if (debug_auto_load)
@@ -489,34 +486,24 @@ file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
       va_end (debug_args);
     }
 
-  back_to = make_cleanup (free_current_contents, &filename_real);
-
   if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
-    {
-      do_cleanups (back_to);
-      return 1;
-    }
+    return 1;
 
   auto_load_safe_path_vec_update ();
   if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
-    {
-      do_cleanups (back_to);
-      return 1;
-    }
+    return 1;
 
   warning (_("File \"%s\" auto-loading has been declined by your "
 	     "`auto-load safe-path' set to \"%s\"."),
-	   filename_real, auto_load_safe_path);
+	   filename_real.get (), auto_load_safe_path);
 
   if (!advice_printed)
     {
       const char *homedir = getenv ("HOME");
-      char *homeinit;
 
       if (homedir == NULL)
 	homedir = "$HOME";
-      homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
-      make_cleanup (xfree, homeinit);
+      std::string homeinit = string_printf ("%s/%s", homedir, gdbinit);
 
       printf_filtered (_("\
 To enable execution of this file add\n\
@@ -528,11 +515,11 @@ line to your configuration file \"%s\".\n\
 For more information about this security protection see the\n\
 \"Auto-loading safe path\" section in the GDB manual.  E.g., run from the shell:\n\
 \tinfo \"(gdb)Auto-loading safe path\"\n"),
-		       filename_real, homeinit, homeinit);
+		       filename_real.get (),
+		       homeinit.c_str (), homeinit.c_str ());
       advice_printed = 1;
     }
 
-  do_cleanups (back_to);
   return 0;
 }
 
@@ -891,30 +878,28 @@ void
 auto_load_objfile_script (struct objfile *objfile,
 			  const struct extension_language_defn *language)
 {
-  char *realname = gdb_realpath (objfile_name (objfile));
-  struct cleanup *cleanups = make_cleanup (xfree, realname);
+  gdb::unique_xmalloc_ptr<char> realname
+    = gdb_realpath (objfile_name (objfile));
 
-  if (!auto_load_objfile_script_1 (objfile, realname, language))
+  if (!auto_load_objfile_script_1 (objfile, realname.get (), language))
     {
       /* For Windows/DOS .exe executables, strip the .exe suffix, so that
 	 FOO-gdb.gdb could be used for FOO.exe, and try again.  */
 
-      size_t len = strlen (realname);
+      size_t len = strlen (realname.get ());
       const size_t lexe = sizeof (".exe") - 1;
 
-      if (len > lexe && strcasecmp (realname + len - lexe, ".exe") == 0)
+      if (len > lexe && strcasecmp (realname.get () + len - lexe, ".exe") == 0)
 	{
 	  len -= lexe;
-	  realname[len] = '\0';
+	  realname.get ()[len] = '\0';
 	  if (debug_auto_load)
 	    fprintf_unfiltered (gdb_stdlog, _("auto-load: Stripped .exe suffix, "
 					      "retrying with \"%s\".\n"),
-				realname);
-	  auto_load_objfile_script_1 (objfile, realname, language);
+				realname.get ());
+	  auto_load_objfile_script_1 (objfile, realname.get (), language);
 	}
     }
-
-  do_cleanups (cleanups);
 }
 
 /* Subroutine of source_section_scripts to simplify it.
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 0e28144..3822850 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -2717,10 +2717,10 @@ dwarf2_get_dwz_file (void)
   std::string abs_storage;
   if (!IS_ABSOLUTE_PATH (filename))
     {
-      char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
+      gdb::unique_xmalloc_ptr<char> abs
+	= gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
 
-      make_cleanup (xfree, abs);
-      abs_storage = ldirname (abs) + SLASH_STRING + filename;
+      abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
       filename = abs_storage.c_str ();
     }
 
@@ -3589,7 +3589,7 @@ dw2_get_real_path (struct objfile *objfile,
 				      qfn->num_file_names, const char *);
 
   if (qfn->real_names[index] == NULL)
-    qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
+    qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
 
   return qfn->real_names[index];
 }
@@ -4383,13 +4383,11 @@ dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
 
   dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
     {
-      const char *this_real_name;
+      gdb::unique_xmalloc_ptr<char> this_real_name;
 
       if (need_fullname)
 	this_real_name = gdb_realpath (filename);
-      else
-	this_real_name = NULL;
-      (*fun) (filename, this_real_name, data);
+      (*fun) (filename, this_real_name.get (), data);
     });
 }
 
diff --git a/gdb/exec.c b/gdb/exec.c
index 45bc133..92c87e4 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -249,16 +249,12 @@ exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty)
 void
 exec_file_attach (const char *filename, int from_tty)
 {
-  struct cleanup *cleanups;
-
   /* First, acquire a reference to the current exec_bfd.  We release
      this at the end of the function; but acquiring it now lets the
      BFD cache return it if this call refers to the same file.  */
   gdb_bfd_ref (exec_bfd);
   gdb_bfd_ref_ptr exec_bfd_holder (exec_bfd);
 
-  cleanups = make_cleanup (null_cleanup, NULL);
-
   /* Remove any previous exec file.  */
   exec_close ();
 
@@ -274,7 +270,7 @@ exec_file_attach (const char *filename, int from_tty)
   else
     {
       int load_via_target = 0;
-      char *scratch_pathname, *canonical_pathname;
+      const char *scratch_pathname, *canonical_pathname;
       int scratch_chan;
       struct target_section *sections = NULL, *sections_end = NULL;
       char **matching;
@@ -287,6 +283,7 @@ exec_file_attach (const char *filename, int from_tty)
 	    load_via_target = 1;
 	}
 
+      gdb::unique_xmalloc_ptr<char> canonical_storage, scratch_storage;
       if (load_via_target)
 	{
 	  /* gdb_bfd_fopen does not support "target:" filenames.  */
@@ -295,19 +292,18 @@ exec_file_attach (const char *filename, int from_tty)
 		       "not supported for %s sysroots"),
 		     TARGET_SYSROOT_PREFIX);
 
-	  scratch_pathname = xstrdup (filename);
-	  make_cleanup (xfree, scratch_pathname);
-
+	  scratch_pathname = filename;
 	  scratch_chan = -1;
-
 	  canonical_pathname = scratch_pathname;
 	}
       else
 	{
+	  char *temp_pathname;
+
 	  scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
 				filename, write_files ?
 				O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
-				&scratch_pathname);
+				&temp_pathname);
 #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
 	  if (scratch_chan < 0)
 	    {
@@ -318,18 +314,19 @@ exec_file_attach (const char *filename, int from_tty)
 				    exename, write_files ?
 				    O_RDWR | O_BINARY
 				    : O_RDONLY | O_BINARY,
-				    &scratch_pathname);
+				    &temp_pathname);
 	    }
 #endif
 	  if (scratch_chan < 0)
 	    perror_with_name (filename);
 
-	  make_cleanup (xfree, scratch_pathname);
+	  scratch_storage.reset (temp_pathname);
+	  scratch_pathname = temp_pathname;
 
 	  /* gdb_bfd_open (and its variants) prefers canonicalized
 	     pathname for better BFD caching.  */
-	  canonical_pathname = gdb_realpath (scratch_pathname);
-	  make_cleanup (xfree, canonical_pathname);
+	  canonical_storage = gdb_realpath (scratch_pathname);
+	  canonical_pathname = canonical_storage.get ();
 	}
 
       gdb_bfd_ref_ptr temp;
@@ -390,8 +387,6 @@ exec_file_attach (const char *filename, int from_tty)
 	(*deprecated_exec_file_display_hook) (filename);
     }
 
-  do_cleanups (cleanups);
-
   bfd_cache_close_all ();
   observer_notify_executable_changed ();
 }
diff --git a/gdb/guile/scm-safe-call.c b/gdb/guile/scm-safe-call.c
index 64a69a3..15bab27 100644
--- a/gdb/guile/scm-safe-call.c
+++ b/gdb/guile/scm-safe-call.c
@@ -438,19 +438,18 @@ gdbscm_safe_source_script (const char *filename)
      %load-path, but we don't want %load-path to be searched.  At least not
      by default.  This function is invoked by the "source" GDB command which
      already has its own path search support.  */
-  char *abs_filename = NULL;
+  gdb::unique_xmalloc_ptr<char> abs_filename;
   const char *result;
 
   if (!IS_ABSOLUTE_PATH (filename))
     {
       abs_filename = gdb_realpath (filename);
-      filename = abs_filename;
+      filename = abs_filename.get ();
     }
 
   result = gdbscm_with_guile (scscm_source_scheme_script,
 			      (void *) filename);
 
-  xfree (abs_filename);
   if (result != NULL)
     return xstrdup (result);
   return NULL;
diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c
index cf68013..6d98135 100644
--- a/gdb/linux-thread-db.c
+++ b/gdb/linux-thread-db.c
@@ -695,7 +695,7 @@ try_thread_db_load (const char *library, int check_auto_load_safe)
 
   /* Do not save system library name, that one is always trusted.  */
   if (strchr (library, '/') != NULL)
-    info->filename = gdb_realpath (library);
+    info->filename = gdb_realpath (library).release ();
 
   if (try_thread_db_load_1 (info))
     return 1;
diff --git a/gdb/main.c b/gdb/main.c
index 886e17f..a0646ed 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -120,7 +120,7 @@ set_gdb_data_directory (const char *new_datadir)
     warning (_("%s is not a directory."), new_datadir);
 
   xfree (gdb_datadir);
-  gdb_datadir = gdb_realpath (new_datadir);
+  gdb_datadir = gdb_realpath (new_datadir).release ();
 
   /* gdb_realpath won't return an absolute path if the path doesn't exist,
      but we still want to record an absolute path here.  If the user entered
@@ -1083,7 +1083,8 @@ captured_main_1 (struct captured_main_args *context)
      the same as the $HOME/.gdbinit file (it should exist, also).  */
   if (local_gdbinit)
     {
-      auto_load_local_gdbinit_pathname = gdb_realpath (local_gdbinit);
+      auto_load_local_gdbinit_pathname
+	= gdb_realpath (local_gdbinit).release ();
 
       if (!inhibit_gdbinit && auto_load_local_gdbinit
 	  && file_is_auto_load_safe (local_gdbinit,
diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index f9959ca..0b53c4b 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -142,7 +142,7 @@ nto_find_and_open_solib (const char *solib, unsigned o_flags,
       if (temp_pathname)
 	{
 	  if (ret >= 0)
-	    *temp_pathname = gdb_realpath (arch_path);
+	    *temp_pathname = gdb_realpath (arch_path).release ();
 	  else
 	    *temp_pathname = NULL;
 	}
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index ff99ca6..c49c7ea 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -376,7 +376,7 @@ struct objfile *
 allocate_objfile (bfd *abfd, const char *name, objfile_flags flags)
 {
   struct objfile *objfile;
-  char *expanded_name;
+  const char *expanded_name;
 
   objfile = XCNEW (struct objfile);
   objfile->psymbol_cache = psymbol_bcache_init ();
diff --git a/gdb/source.c b/gdb/source.c
index e2a507d..0453f92 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -911,7 +911,7 @@ done:
       if (fd < 0)
 	*filename_opened = NULL;
       else if ((opts & OPF_RETURN_REALPATH) != 0)
-	*filename_opened = gdb_realpath (filename);
+	*filename_opened = gdb_realpath (filename).release ();
       else
 	*filename_opened = gdb_abspath (filename).release ();
     }
@@ -1050,7 +1050,7 @@ find_and_open_source (const char *filename,
       result = gdb_open_cloexec (*fullname, OPEN_MODE, 0);
       if (result >= 0)
 	{
-	  char *lpath = gdb_realpath (*fullname);
+	  char *lpath = gdb_realpath (*fullname).release ();
 
 	  xfree (*fullname);
 	  *fullname = lpath;
diff --git a/gdb/symtab.c b/gdb/symtab.c
index ccf31cc..2babe7f 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -468,7 +468,7 @@ iterate_over_symtabs (const char *name,
      absolutizing a relative path.  */
   if (IS_ABSOLUTE_PATH (name))
     {
-      real_path.reset (gdb_realpath (name));
+      real_path = gdb_realpath (name);
       gdb_assert (IS_ABSOLUTE_PATH (real_path.get ()));
     }
 
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index d188f83..78e7940 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2017-08-18  Tom Tromey  <tom@tromey.com>
+
+	* gdb.gdb/xfullpath.exp: Remove.
+
 2017-08-18  Yao Qi  <yao.qi@linaro.org>
 
 	* gdb.server/unittest.exp: New.
diff --git a/gdb/testsuite/gdb.gdb/xfullpath.exp b/gdb/testsuite/gdb.gdb/xfullpath.exp
deleted file mode 100644
index 8c90693..0000000
--- a/gdb/testsuite/gdb.gdb/xfullpath.exp
+++ /dev/null
@@ -1,60 +0,0 @@
-# Copyright 2002-2017 Free Software Foundation, Inc.
-
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-# This file was written by Joel Brobecker. (brobecker@gnat.com), derived
-# from selftest.exp, written by Rob Savoye.
-
-load_lib selftest-support.exp
-
-proc test_with_self {} {
-    # A file which contains a directory prefix
-    gdb_test "print gdb_realpath (\"./xfullpath.exp\")" \
-             ".\[0-9\]+ =.*\".*/xfullpath.exp\"" \
-             "A filename with ./ as the directory prefix"
-
-    # A file which contains a directory prefix
-    gdb_test "print gdb_realpath (\"../../defs.h\")" \
-             ".\[0-9\]+ =.*\".*/defs.h\"" \
-             "A filename with ../ in the directory prefix"
-
-    # A one-character filename
-    gdb_test "print gdb_realpath (\"./a\")" \
-             ".\[0-9\]+ =.*\".*/a\"" \
-             "A one-char filename in the current directory"
-          
-    # A file in the root directory
-    gdb_test "print gdb_realpath (\"/root_file_which_should_exist\")" \
-             ".\[0-9\]+ =.*\"/root_file_which_should_exist\"" \
-             "A filename in the root directory"
-          
-    # A file which does not have a directory prefix
-    gdb_test "print gdb_realpath (\"xfullpath.exp\")" \
-             ".\[0-9\]+ =.*\"xfullpath.exp\"" \
-             "A filename without any directory prefix"
-
-    # A one-char filename without any directory prefix
-    gdb_test "print gdb_realpath (\"a\")" \
-             ".\[0-9\]+ =.*\"a\"" \
-             "A one-char filename without any directory prefix"
-
-    # An empty filename
-    gdb_test "print gdb_realpath (\"\")" \
-             ".\[0-9\]+ =.*\"\"" \
-             "an empty filename"
-
-    return 0
-}
-
-do_self_tests captured_main test_with_self
diff --git a/gdb/utils.c b/gdb/utils.c
index 5bdc638..3ca29b7 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -66,6 +66,7 @@
 #include "interps.h"
 #include "gdb_regex.h"
 #include "job-control.h"
+#include "common/selftest.h"
 
 #if !HAVE_DECL_MALLOC
 extern PTR malloc ();		/* ARI: PTR */
@@ -2662,7 +2663,7 @@ string_to_core_addr (const char *my_string)
   return addr;
 }
 
-char *
+gdb::unique_xmalloc_ptr<char>
 gdb_realpath (const char *filename)
 {
 /* On most hosts, we rely on canonicalize_file_name to compute
@@ -2698,21 +2699,57 @@ gdb_realpath (const char *filename)
        we might not be able to display the original casing in a given
        path.  */
     if (len > 0 && len < MAX_PATH)
-      return xstrdup (buf);
+      return gdb::unique_xmalloc_ptr<char> (xstrdup (buf));
   }
 #else
   {
     char *rp = canonicalize_file_name (filename);
 
     if (rp != NULL)
-      return rp;
+      return gdb::unique_xmalloc_ptr<char> (rp);
   }
 #endif
 
   /* This system is a lost cause, just dup the buffer.  */
-  return xstrdup (filename);
+  return gdb::unique_xmalloc_ptr<char> (xstrdup (filename));
 }
 
+#if GDB_SELF_TEST
+
+static void
+gdb_realpath_check_trailer (const char *input, const char *trailer)
+{
+  gdb::unique_xmalloc_ptr<char> result = gdb_realpath (input);
+
+  size_t len = strlen (result.get ());
+  size_t trail_len = strlen (trailer);
+
+  SELF_CHECK (len >= trail_len
+	      && strcmp (result.get () + len - trail_len, trailer) == 0);
+}
+
+static void
+gdb_realpath_tests ()
+{
+  /* A file which contains a directory prefix.  */
+  gdb_realpath_check_trailer ("./xfullpath.exp", "/xfullpath.exp");
+  /* A file which contains a directory prefix.  */
+  gdb_realpath_check_trailer ("../../defs.h", "/defs.h");
+  /* A one-character filename.  */
+  gdb_realpath_check_trailer ("./a", "/a");
+  /* A file in the root directory.  */
+  gdb_realpath_check_trailer ("/root_file_which_should_exist",
+			      "/root_file_which_should_exist");
+  /* A file which does not have a directory prefix.  */
+  gdb_realpath_check_trailer ("xfullpath.exp", "xfullpath.exp");
+  /* A one-char filename without any directory prefix.  */
+  gdb_realpath_check_trailer ("a", "a");
+  /* An empty filename.  */
+  gdb_realpath_check_trailer ("", "");
+}
+
+#endif /* GDB_SELF_TEST */
+
 /* Return a copy of FILENAME, with its directory prefix canonicalized
    by gdb_realpath.  */
 
@@ -2721,7 +2758,6 @@ gdb_realpath_keepfile (const char *filename)
 {
   const char *base_name = lbasename (filename);
   char *dir_name;
-  char *real_path;
   char *result;
 
   /* Extract the basename of filename, and return immediately 
@@ -2749,13 +2785,13 @@ gdb_realpath_keepfile (const char *filename)
   /* Canonicalize the directory prefix, and build the resulting
      filename.  If the dirname realpath already contains an ending
      directory separator, avoid doubling it.  */
-  real_path = gdb_realpath (dir_name);
+  gdb::unique_xmalloc_ptr<char> path_storage = gdb_realpath (dir_name);
+  const char *real_path = path_storage.get ();
   if (IS_DIR_SEPARATOR (real_path[strlen (real_path) - 1]))
     result = concat (real_path, base_name, (char *) NULL);
   else
     result = concat (real_path, SLASH_STRING, base_name, (char *) NULL);
 
-  xfree (real_path);
   return gdb::unique_xmalloc_ptr<char> (result);
 }
 
@@ -3283,4 +3319,8 @@ _initialize_utils (void)
   add_internal_problem_command (&internal_error_problem);
   add_internal_problem_command (&internal_warning_problem);
   add_internal_problem_command (&demangler_warning_problem);
+
+#if GDB_SELF_TEST
+  selftests::register_test (gdb_realpath_tests);
+#endif
 }
diff --git a/gdb/utils.h b/gdb/utils.h
index 477257b..3ceefc1 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -252,7 +252,7 @@ extern struct cleanup *make_bpstat_clear_actions_cleanup (void);
 \f
 /* Path utilities.  */
 
-extern char *gdb_realpath (const char *);
+extern gdb::unique_xmalloc_ptr<char> gdb_realpath (const char *);
 
 extern gdb::unique_xmalloc_ptr<char> gdb_realpath_keepfile (const char *);
 
-- 
2.9.4

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

* Re: [RFA 0/5] simple string changes
  2017-08-18 20:01 [RFA 0/5] simple string changes Tom Tromey
                   ` (4 preceding siblings ...)
  2017-08-18 20:01 ` [RFA 1/5] Change gdb_abspath " Tom Tromey
@ 2017-08-21  9:57 ` Pedro Alves
  5 siblings, 0 replies; 7+ messages in thread
From: Pedro Alves @ 2017-08-21  9:57 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

On 08/18/2017 09:00 PM, Tom Tromey wrote:
> I had sent this patch a little while ago, but it was lost in the
> sourceware crash.
> 
> This series fixes a few spots doing manual management of a char* to
> use gdb::unique_xmalloc_ptr or std::string, as appropriate.
> 
> Regression tested on the buildbot.

LGTM.

Thanks,
Pedro Alves

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

end of thread, other threads:[~2017-08-21  9:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-18 20:01 [RFA 0/5] simple string changes Tom Tromey
2017-08-18 20:01 ` [RFA 3/5] Change gdb_realpath to return a unique_xmalloc_ptr Tom Tromey
2017-08-18 20:01 ` [RFA 2/5] Change gdb_realpath_keepfile " Tom Tromey
2017-08-18 20:01 ` [RFA 4/5] Change rewrite_source_path " Tom Tromey
2017-08-18 20:01 ` [RFA 5/5] Change psymtab_search_name " Tom Tromey
2017-08-18 20:01 ` [RFA 1/5] Change gdb_abspath " Tom Tromey
2017-08-21  9:57 ` [RFA 0/5] simple string changes Pedro Alves

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).