This is the mail archive of the gdb-cvs@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[binutils-gdb] Refactor handle_qxfer_libraries and friends to use std::string


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

commit 04fd3ba9b63936653fff2b1657b3824c94a8efd6
Author: Simon Marchi <simon.marchi@ericsson.com>
Date:   Sat Sep 16 14:22:11 2017 +0200

    Refactor handle_qxfer_libraries and friends to use std::string
    
    Using std::string in handle_qxfer_libraries and friends allow to
    simplify the code.  We don't have to manually free the buffer, and we
    don't have to pre-compute the required space.
    
    gdb/gdbserver/ChangeLog:
    
    	* server.c (accumulate_file_name_length): Remove.
    	(emit_dll_description): Adjust to std::string change.
    	(handle_qxfer_libraries): Use std::string to hold document.

Diff:
---
 gdb/gdbserver/ChangeLog |  6 +++++
 gdb/gdbserver/server.c  | 68 ++++++++++---------------------------------------
 2 files changed, 19 insertions(+), 55 deletions(-)

diff --git a/gdb/gdbserver/ChangeLog b/gdb/gdbserver/ChangeLog
index 49a76c9..00e7891 100644
--- a/gdb/gdbserver/ChangeLog
+++ b/gdb/gdbserver/ChangeLog
@@ -1,5 +1,11 @@
 2017-09-16  Simon Marchi  <simon.marchi@ericsson.com>
 
+	* server.c (accumulate_file_name_length): Remove.
+	(emit_dll_description): Adjust to std::string change.
+	(handle_qxfer_libraries): Use std::string to hold document.
+
+2017-09-16  Simon Marchi  <simon.marchi@ericsson.com>
+
 	* linux-low.c (linux_qxfer_libraries_svr4): Adjust to change of
 	return type of xml_escape_text.
 	* server.c (emit_dll_description): Likewise.
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 92943e2..f4faff9 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -1509,43 +1509,18 @@ handle_qxfer_features (const char *annex,
 }
 
 /* Worker routine for handle_qxfer_libraries.
-   Add to the length pointed to by ARG a conservative estimate of the
-   length needed to transmit the file name of INF.  */
-
-static void
-accumulate_file_name_length (struct inferior_list_entry *inf, void *arg)
-{
-  struct dll_info *dll = (struct dll_info *) inf;
-  unsigned int *total_len = (unsigned int *) arg;
-
-  /* Over-estimate the necessary memory.  Assume that every character
-     in the library name must be escaped.  */
-  *total_len += 128 + 6 * strlen (dll->name);
-}
-
-/* Worker routine for handle_qxfer_libraries.
    Emit the XML to describe the library in INF.  */
 
 static void
 emit_dll_description (struct inferior_list_entry *inf, void *arg)
 {
   struct dll_info *dll = (struct dll_info *) inf;
-  char **p_ptr = (char **) arg;
-  char *p = *p_ptr;
-
-  strcpy (p, "  <library name=\"");
-  p = p + strlen (p);
+  std::string *document = (std::string *) arg;
   std::string name = xml_escape_text (dll->name);
-  strcpy (p, name.c_str ());
-  p = p + strlen (p);
-  strcpy (p, "\"><segment address=\"");
-  p = p + strlen (p);
-  sprintf (p, "0x%lx", (long) dll->base_addr);
-  p = p + strlen (p);
-  strcpy (p, "\"/></library>\n");
-  p = p + strlen (p);
-
-  *p_ptr = p;
+
+  *document += string_printf
+    ("  <library name=\"%s\"><segment address=\"0x%lx\"/></library>\n",
+     name.c_str (), (long) dll->base_addr);
 }
 
 /* Handle qXfer:libraries:read.  */
@@ -1555,43 +1530,26 @@ handle_qxfer_libraries (const char *annex,
 			gdb_byte *readbuf, const gdb_byte *writebuf,
 			ULONGEST offset, LONGEST len)
 {
-  unsigned int total_len;
-  char *document, *p;
-
   if (writebuf != NULL)
     return -2;
 
   if (annex[0] != '\0' || current_thread == NULL)
     return -1;
 
-  total_len = 64;
-  for_each_inferior_with_data (&all_dlls, accumulate_file_name_length,
-			       &total_len);
-
-  document = (char *) malloc (total_len);
-  if (document == NULL)
-    return -1;
-
-  strcpy (document, "<library-list version=\"1.0\">\n");
-  p = document + strlen (document);
+  std::string document = "<library-list version=\"1.0\">\n";
 
-  for_each_inferior_with_data (&all_dlls, emit_dll_description, &p);
+  for_each_inferior_with_data (&all_dlls, emit_dll_description, &document);
 
-  strcpy (p, "</library-list>\n");
+  document += "</library-list>\n";
 
-  total_len = strlen (document);
+  if (offset > document.length ())
+    return -1;
 
-  if (offset > total_len)
-    {
-      free (document);
-      return -1;
-    }
+  if (offset + len > document.length ())
+    len = document.length () - offset;
 
-  if (offset + len > total_len)
-    len = total_len - offset;
+  memcpy (readbuf, &document[offset], len);
 
-  memcpy (readbuf, document + offset, len);
-  free (document);
   return len;
 }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]