[binutils-gdb] Use std::string to simplify build_id_to_debug_bfd

Simon Marchi simark@sourceware.org
Thu Mar 8 23:58:00 GMT 2018


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=00b400574aa75c1c5fe469233ab16930e2d8a4c8

commit 00b400574aa75c1c5fe469233ab16930e2d8a4c8
Author: Simon Marchi <simon.marchi@polymtl.ca>
Date:   Thu Mar 8 18:57:53 2018 -0500

    Use std::string to simplify build_id_to_debug_bfd
    
    Using std::string here makes the string building simpler thank playing
    with char*.  A stack allocation is replaced with heap allocation, but
    I don't think this is really performance-critical code.
    
    gdb/ChangeLog:
    
    	* build-id.c (build_id_to_debug_bfd): Use std::string.

Diff:
---
 gdb/ChangeLog  |  4 ++++
 gdb/build-id.c | 33 +++++++++++----------------------
 2 files changed, 15 insertions(+), 22 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 7e4bd95..fc5738b 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,9 @@
 2018-03-08  Simon Marchi  <simon.marchi@polymtl.ca>
 
+	* build-id.c (build_id_to_debug_bfd): Use std::string.
+
+2018-03-08  Simon Marchi  <simon.marchi@polymtl.ca>
+
 	* build-id.c (find_separate_debug_file_by_buildid): Return
 	std::string.
 	* build-id.h (find_separate_debug_file_by_buildid): Return
diff --git a/gdb/build-id.c b/gdb/build-id.c
index a5d4e67..c8eacbd 100644
--- a/gdb/build-id.c
+++ b/gdb/build-id.c
@@ -70,16 +70,7 @@ build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
 gdb_bfd_ref_ptr
 build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
 {
-  char *link, *debugdir;
-  int ix;
   gdb_bfd_ref_ptr abfd;
-  int alloc_len;
-
-  /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
-  alloc_len = (strlen (debug_file_directory)
-	       + (sizeof "/.build-id/" - 1) + 1
-	       + 2 * build_id_len + (sizeof ".debug" - 1) + 1);
-  link = (char *) alloca (alloc_len);
 
   /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
      cause "/.build-id/..." lookups.  */
@@ -89,32 +80,30 @@ build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
 
   for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
     {
-      size_t debugdir_len = strlen (debugdir.get ());
       const gdb_byte *data = build_id;
       size_t size = build_id_len;
-      char *s;
 
-      memcpy (link, debugdir.get (), debugdir_len);
-      s = &link[debugdir_len];
-      s += sprintf (s, "/.build-id/");
+      std::string link = debugdir.get ();
+      link += "/.build-id/";
+
       if (size > 0)
 	{
 	  size--;
-	  s += sprintf (s, "%02x", (unsigned) *data++);
+	  string_appendf (link, "%02x/", (unsigned) *data++);
 	}
-      if (size > 0)
-	*s++ = '/';
+
       while (size-- > 0)
-	s += sprintf (s, "%02x", (unsigned) *data++);
-      strcpy (s, ".debug");
+	string_appendf (link, "%02x", (unsigned) *data++);
+
+      link += ".debug";
 
       if (separate_debug_file_debug)
-	printf_unfiltered (_("  Trying %s\n"), link);
+	printf_unfiltered (_("  Trying %s\n"), link.c_str ());
 
       /* lrealpath() is expensive even for the usually non-existent files.  */
       gdb::unique_xmalloc_ptr<char> filename;
-      if (access (link, F_OK) == 0)
-	filename.reset (lrealpath (link));
+      if (access (link.c_str (), F_OK) == 0)
+	filename.reset (lrealpath (link.c_str ()));
 
       if (filename == NULL)
 	continue;



More information about the Gdb-cvs mailing list