Change auto_load_objfile_script_1 to use std::string
authorTom Tromey <tom@tromey.com>
Fri, 16 Mar 2018 23:02:11 +0000 (17:02 -0600)
committerTom Tromey <tom@tromey.com>
Sat, 17 Mar 2018 19:28:56 +0000 (13:28 -0600)
This replaces some manual string manipulation in
auto_load_objfile_script_1 with std::string, simplifying the code and
allowing the removal of some cleanups.

Tested by the buildbot.

2018-03-17  Tom Tromey  <tom@tromey.com>

* auto-load.c (auto_load_objfile_script_1): Use std::string.

gdb/ChangeLog
gdb/auto-load.c

index 086d117..4ab82d8 100644 (file)
@@ -1,5 +1,9 @@
 2018-03-17  Tom Tromey  <tom@tromey.com>
 
+       * auto-load.c (auto_load_objfile_script_1): Use std::string.
+
+2018-03-17  Tom Tromey  <tom@tromey.com>
+
        * target.c (class scoped_target_fd): New.
        (target_fileio_close_cleanup): Remove.
        (target_fileio_read_alloc_1): Use scoped_target_fd.
index 70bddbc..09ecb87 100644 (file)
@@ -769,24 +769,19 @@ static int
 auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
                            const struct extension_language_defn *language)
 {
-  char *filename, *debugfile;
-  int len, retval;
-  struct cleanup *cleanups;
+  const char *debugfile;
+  int retval;
   const char *suffix = ext_lang_auto_load_suffix (language);
 
-  len = strlen (realname);
-  filename = (char *) xmalloc (len + strlen (suffix) + 1);
-  memcpy (filename, realname, len);
-  strcpy (filename + len, suffix);
+  std::string filename = std::string (realname) + suffix;
 
-  cleanups = make_cleanup (xfree, filename);
-
-  gdb_file_up input = gdb_fopen_cloexec (filename, "r");
-  debugfile = filename;
+  gdb_file_up input = gdb_fopen_cloexec (filename.c_str (), "r");
+  debugfile = filename.c_str ();
   if (debug_auto_load)
     fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file \"%s\" %s.\n"),
                        debugfile, input ? _("exists") : _("does not exist"));
 
+  std::string debugfile_holder;
   if (!input)
     {
       /* Also try the same file in a subdirectory of gdb's data
@@ -802,14 +797,10 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
 
       for (const gdb::unique_xmalloc_ptr<char> &dir : vec)
        {
-         debugfile = (char *) xmalloc (strlen (dir.get ())
-                                       + strlen (filename) + 1);
-         strcpy (debugfile, dir.get ());
-
          /* FILENAME is absolute, so we don't need a "/" here.  */
-         strcat (debugfile, filename);
+         debugfile_holder = dir.get () + filename;
+         debugfile = debugfile_holder.c_str ();
 
-         make_cleanup (xfree, debugfile);
          input = gdb_fopen_cloexec (debugfile, "r");
          if (debug_auto_load)
            fprintf_unfiltered (gdb_stdlog, _("auto-load: Attempted file "
@@ -862,7 +853,6 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
   else
     retval = 0;
 
-  do_cleanups (cleanups);
   return retval;
 }