public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 3/4] Return std::string from gdb_bfd_errmsg
Date: Mon, 03 Sep 2018 19:03:00 -0000	[thread overview]
Message-ID: <20180903190250.11599-4-tom@tromey.com> (raw)
In-Reply-To: <20180903190250.11599-1-tom@tromey.com>

This changes gdb_bfd_errmsg to return a std::string, removing a
cleanup.  This approach may be slightly less efficient than the
previous code, but I don't believe this is very important in this
situation.

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

	* utils.h (gdb_bfd_errmsg): Return std::string.
	* exec.c (exec_file_attach): Update.
	* compile/compile-object-load.c (compile_object_load): Update.
	* utils.c (gdb_bfd_errmsg): Return std::string.
---
 gdb/ChangeLog                     |  7 +++++++
 gdb/compile/compile-object-load.c |  3 ++-
 gdb/exec.c                        |  2 +-
 gdb/utils.c                       | 27 +++++++--------------------
 gdb/utils.h                       |  2 +-
 5 files changed, 18 insertions(+), 23 deletions(-)

diff --git a/gdb/compile/compile-object-load.c b/gdb/compile/compile-object-load.c
index 873750b9440..40053d281a1 100644
--- a/gdb/compile/compile-object-load.c
+++ b/gdb/compile/compile-object-load.c
@@ -638,7 +638,8 @@ compile_object_load (const compile_file_names &file_names,
 
   if (!bfd_check_format_matches (abfd.get (), bfd_object, &matching))
     error (_("\"%s\": not in loadable format: %s"),
-          filename.get (), gdb_bfd_errmsg (bfd_get_error (), matching));
+	   filename.get (),
+	   gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
 
   if ((bfd_get_file_flags (abfd.get ()) & (EXEC_P | DYNAMIC)) != 0)
     error (_("\"%s\": not in object format."), filename.get ());
diff --git a/gdb/exec.c b/gdb/exec.c
index 3023ff7e5aa..6e44b0e821b 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -362,7 +362,7 @@ exec_file_attach (const char *filename, int from_tty)
 	  exec_close ();
 	  error (_("\"%s\": not in executable format: %s"),
 		 scratch_pathname,
-		 gdb_bfd_errmsg (bfd_get_error (), matching));
+		 gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
 	}
 
       if (build_section_table (exec_bfd, &sections, &sections_end))
diff --git a/gdb/utils.c b/gdb/utils.c
index 7a8c80c64ed..d7980fe3a18 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -2921,39 +2921,26 @@ compare_positive_ints (const void *ap, const void *bp)
 #define AMBIGUOUS_MESS2	\
   ".\nUse \"set gnutarget format-name\" to specify the format."
 
-const char *
+std::string
 gdb_bfd_errmsg (bfd_error_type error_tag, char **matching)
 {
-  char *ret, *retp;
-  int ret_len;
   char **p;
 
   /* Check if errmsg just need simple return.  */
   if (error_tag != bfd_error_file_ambiguously_recognized || matching == NULL)
     return bfd_errmsg (error_tag);
 
-  ret_len = strlen (bfd_errmsg (error_tag)) + strlen (AMBIGUOUS_MESS1)
-            + strlen (AMBIGUOUS_MESS2);
-  for (p = matching; *p; p++)
-    ret_len += strlen (*p) + 1;
-  ret = (char *) xmalloc (ret_len + 1);
-  retp = ret;
-  make_cleanup (xfree, ret);
-
-  strcpy (retp, bfd_errmsg (error_tag));
-  retp += strlen (retp);
-
-  strcpy (retp, AMBIGUOUS_MESS1);
-  retp += strlen (retp);
+  std::string ret (bfd_errmsg (error_tag));
+  ret += AMBIGUOUS_MESS1;
 
   for (p = matching; *p; p++)
     {
-      sprintf (retp, " %s", *p);
-      retp += strlen (retp);
+      ret += " ";
+      ret += *p;
     }
-  xfree (matching);
+  ret += AMBIGUOUS_MESS2;
 
-  strcpy (retp, AMBIGUOUS_MESS2);
+  xfree (matching);
 
   return ret;
 }
diff --git a/gdb/utils.h b/gdb/utils.h
index 68523994b94..fa9a59087da 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -115,7 +115,7 @@ compare_cstrings (const char *str1, const char *str2)
    MATCHING, if non-NULL, is the corresponding argument to
    bfd_check_format_matches, and will be freed.  */
 
-extern const char *gdb_bfd_errmsg (bfd_error_type error_tag, char **matching);
+extern std::string gdb_bfd_errmsg (bfd_error_type error_tag, char **matching);
 
 /* Reset the prompt_for_continue clock.  */
 void reset_prompt_for_continue_wait_time (void);
-- 
2.13.6

  parent reply	other threads:[~2018-09-03 19:03 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-03 19:03 [PATCH 0/4] Some cleanup removal Tom Tromey
2018-09-03 19:02 ` [PATCH 1/4] Remove cleanup from add_path Tom Tromey
2018-09-03 19:03 ` [PATCH 4/4] Remove cleanup from try_open_exec_file Tom Tromey
2018-09-03 19:03 ` Tom Tromey [this message]
2018-09-03 19:03 ` [PATCH 2/4] Remove cleanup from procfs.c Tom Tromey
2018-09-04 10:50   ` Rainer Orth
2018-09-04 16:56     ` Tom Tromey
2018-09-04  9:37 ` [PATCH 0/4] Some cleanup removal Rainer Orth
2018-09-04 16:58   ` Tom Tromey
2018-09-04 18:30     ` Rainer Orth
2018-09-13 22:41 ` Tom Tromey

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=20180903190250.11599-4-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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).