[RFA,04/13] Use unique_xmalloc_ptr in find_separate_debug_file_by_debuglink

Message ID 20171102223612.3642-5-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Nov. 2, 2017, 10:36 p.m. UTC
  This changes find_separate_debug_file_by_debuglink to use
unique_xmalloc_ptr, removing some cleanups.

gdb/ChangeLog
2017-11-02  Tom Tromey  <tom@tromey.com>

	* symfile.c (find_separate_debug_file_by_debuglink): Use
	unique_xmalloc_ptr.
---
 gdb/ChangeLog |  5 +++++
 gdb/symfile.c | 36 ++++++++++++++----------------------
 2 files changed, 19 insertions(+), 22 deletions(-)
  

Comments

Simon Marchi Nov. 3, 2017, 1:02 a.m. UTC | #1
On 2017-11-02 18:36, Tom Tromey wrote:
> @@ -1580,19 +1575,17 @@ find_separate_debug_file_by_debuglink (struct
> objfile *objfile)
>        if (lstat (objfile_name (objfile), &st_buf) == 0
>  	  && S_ISLNK (st_buf.st_mode))
>  	{
> -	  char *symlink_dir;
> -
> -	  symlink_dir = lrealpath (objfile_name (objfile));
> +	  gdb::unique_xmalloc_ptr<char> symlink_dir
> +	    (lrealpath (objfile_name (objfile)));
>  	  if (symlink_dir != NULL)
>  	    {
> -	      make_cleanup (xfree, symlink_dir);
> -	      terminate_after_last_dir_separator (symlink_dir);
> -	      if (strcmp (dir, symlink_dir) != 0)
> +	      terminate_after_last_dir_separator (symlink_dir.get ());
> +	      if (strcmp (dir.c_str (), symlink_dir.get ()) != 0)

Just one tiny nit: when possible, I like to replace strcmp with 
std::string's operator==

   if (dir == symlink_dir.get ())

but I'll leave it up to you.

Simon
  
Tom Tromey Nov. 3, 2017, 4:37 p.m. UTC | #2
>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:

Simon> Just one tiny nit: when possible, I like to replace strcmp with
Simon> std::string's operator==
Simon>   if (dir == symlink_dir.get ())
Simon> but I'll leave it up to you.

Good idea, I made this change; though I think it should be != due to:

> +	      if (strcmp (dir.c_str (), symlink_dir.get ()) != 0)

Tom
  

Patch

diff --git a/gdb/symfile.c b/gdb/symfile.c
index 69358e44ce..fb63441ac0 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -1545,13 +1545,11 @@  terminate_after_last_dir_separator (char *path)
 char *
 find_separate_debug_file_by_debuglink (struct objfile *objfile)
 {
-  char *debuglink;
-  char *dir, *canon_dir;
   char *debugfile;
   unsigned long crc32;
-  struct cleanup *cleanups;
 
-  debuglink = bfd_get_debug_link_info (objfile->obfd, &crc32);
+  gdb::unique_xmalloc_ptr<char> debuglink
+    (bfd_get_debug_link_info (objfile->obfd, &crc32));
 
   if (debuglink == NULL)
     {
@@ -1560,15 +1558,12 @@  find_separate_debug_file_by_debuglink (struct objfile *objfile)
       return NULL;
     }
 
-  cleanups = make_cleanup (xfree, debuglink);
-  dir = xstrdup (objfile_name (objfile));
-  make_cleanup (xfree, dir);
-  terminate_after_last_dir_separator (dir);
-  canon_dir = lrealpath (dir);
+  std::string dir = objfile_name (objfile);
+  terminate_after_last_dir_separator (&dir[0]);
+  gdb::unique_xmalloc_ptr<char> canon_dir (lrealpath (dir.c_str ()));
 
-  debugfile = find_separate_debug_file (dir, canon_dir, debuglink,
-					crc32, objfile);
-  xfree (canon_dir);
+  debugfile = find_separate_debug_file (dir.c_str (), canon_dir.get (),
+					debuglink.get (), crc32, objfile);
 
   if (debugfile == NULL)
     {
@@ -1580,19 +1575,17 @@  find_separate_debug_file_by_debuglink (struct objfile *objfile)
       if (lstat (objfile_name (objfile), &st_buf) == 0
 	  && S_ISLNK (st_buf.st_mode))
 	{
-	  char *symlink_dir;
-
-	  symlink_dir = lrealpath (objfile_name (objfile));
+	  gdb::unique_xmalloc_ptr<char> symlink_dir
+	    (lrealpath (objfile_name (objfile)));
 	  if (symlink_dir != NULL)
 	    {
-	      make_cleanup (xfree, symlink_dir);
-	      terminate_after_last_dir_separator (symlink_dir);
-	      if (strcmp (dir, symlink_dir) != 0)
+	      terminate_after_last_dir_separator (symlink_dir.get ());
+	      if (strcmp (dir.c_str (), symlink_dir.get ()) != 0)
 		{
 		  /* Different directory, so try using it.  */
-		  debugfile = find_separate_debug_file (symlink_dir,
-							symlink_dir,
-							debuglink,
+		  debugfile = find_separate_debug_file (symlink_dir.get (),
+							symlink_dir.get (),
+							debuglink.get (),
 							crc32,
 							objfile);
 		}
@@ -1600,7 +1593,6 @@  find_separate_debug_file_by_debuglink (struct objfile *objfile)
 	}
     }
 
-  do_cleanups (cleanups);
   return debugfile;
 }