Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [RFA] Change auto_load_objfile_script_1 to use std::string
Date: Sat, 17 Mar 2018 15:46:00 -0000	[thread overview]
Message-ID: <20180317154638.1370-1-tom@tromey.com> (raw)

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.

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

	* auto-load.c (auto_load_objfile_script_1): Use std::string.
---
 gdb/ChangeLog   |  4 ++++
 gdb/auto-load.c | 26 ++++++++------------------
 2 files changed, 12 insertions(+), 18 deletions(-)

diff --git a/gdb/auto-load.c b/gdb/auto-load.c
index 70bddbc862..09ecb87e13 100644
--- a/gdb/auto-load.c
+++ b/gdb/auto-load.c
@@ -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;
 }
 
-- 
2.13.6


             reply	other threads:[~2018-03-17 15:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-17 15:46 Tom Tromey [this message]
2018-03-17 17:36 ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180317154638.1370-1-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox