From: Simon Marchi Date: Sat, 16 Sep 2017 12:19:31 +0000 (+0200) Subject: Make xml_escape_text return an std::string X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=5e1875543df7413d3cbc3831390445e347064b75;p=binutils-gdb.git Make xml_escape_text return an std::string This is a simple replacement, it allows removing some manual free'ing in the callers. gdb/ChangeLog: * common/buffer.c (buffer_xml_printf): Adjust. * common/xml-utils.c (xml_escape_text): Change return type to std::string, update code accordingly. * common/xml-utils.h (xml_escape_text): Change return type to std::string. * rs6000-aix-tdep.c (rs6000_aix_shared_library_to_xml): Adjust. * windows-tdep.c (windows_xfer_shared_library): Adjust. * unittests/xml-utils-selftests.c (test_xml_escape_text): Adjust. gdb/gdbserver/ChangeLog: * 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/ChangeLog b/gdb/ChangeLog index 21c3694bc5c..7d2fb4970eb 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,15 @@ +2017-09-16 Simon Marchi + + * common/buffer.c (buffer_xml_printf): Adjust. + * common/xml-utils.c (xml_escape_text): Change return type to + std::string, update code accordingly. + * common/xml-utils.h (xml_escape_text): Change return type to + std::string. + * rs6000-aix-tdep.c (rs6000_aix_shared_library_to_xml): Adjust. + * windows-tdep.c (windows_xfer_shared_library): Adjust. + * unittests/xml-utils-selftests.c (test_xml_escape_text): + Adjust. + 2017-09-16 Simon Marchi * Makefile.in (SUBDIR_UNITTESTS_SRCS): Add new source file. diff --git a/gdb/common/buffer.c b/gdb/common/buffer.c index c5eb16904fb..70d91e6182c 100644 --- a/gdb/common/buffer.c +++ b/gdb/common/buffer.c @@ -88,7 +88,6 @@ buffer_xml_printf (struct buffer *buffer, const char *format, ...) if (percent) { char buf[32]; - char *p; char *str = buf; const char *f_old = f; @@ -163,9 +162,8 @@ buffer_xml_printf (struct buffer *buffer, const char *format, ...) if (str) { buffer_grow (buffer, prev, f_old - prev - 1); - p = xml_escape_text (str); - buffer_grow_str (buffer, p); - xfree (p); + std::string p = xml_escape_text (str); + buffer_grow_str (buffer, p.c_str ()); prev = f + 1; } percent = 0; diff --git a/gdb/common/xml-utils.c b/gdb/common/xml-utils.c index b5fe442d75f..c6dd2fb5ce1 100644 --- a/gdb/common/xml-utils.c +++ b/gdb/common/xml-utils.c @@ -20,64 +20,37 @@ #include "common-defs.h" #include "xml-utils.h" -/* Return a malloc allocated string with special characters from TEXT - replaced by entity references. */ +/* Return a string with special characters from TEXT replaced by entity + references. */ -char * +std::string xml_escape_text (const char *text) { - char *result; - int i, special; - - /* Compute the length of the result. */ - for (i = 0, special = 0; text[i] != '\0'; i++) - switch (text[i]) - { - case '\'': - case '\"': - special += 5; - break; - case '&': - special += 4; - break; - case '<': - case '>': - special += 3; - break; - default: - break; - } + std::string result; /* Expand the result. */ - result = (char *) xmalloc (i + special + 1); - for (i = 0, special = 0; text[i] != '\0'; i++) + for (int i = 0; text[i] != '\0'; i++) switch (text[i]) { case '\'': - strcpy (result + i + special, "'"); - special += 5; + result += "'"; break; case '\"': - strcpy (result + i + special, """); - special += 5; + result += """; break; case '&': - strcpy (result + i + special, "&"); - special += 4; + result += "&"; break; case '<': - strcpy (result + i + special, "<"); - special += 3; + result += "<"; break; case '>': - strcpy (result + i + special, ">"); - special += 3; + result += ">"; break; default: - result[i + special] = text[i]; + result += text[i]; break; } - result[i + special] = '\0'; return result; } diff --git a/gdb/common/xml-utils.h b/gdb/common/xml-utils.h index d138bab51f2..f69f5f5178e 100644 --- a/gdb/common/xml-utils.h +++ b/gdb/common/xml-utils.h @@ -23,6 +23,6 @@ /* Return a malloc allocated string with special characters from TEXT replaced by entity references. */ -extern char *xml_escape_text (const char *text); +extern std::string xml_escape_text (const char *text); #endif diff --git a/gdb/gdbserver/ChangeLog b/gdb/gdbserver/ChangeLog index 61b2e0671d9..49a76c94f60 100644 --- a/gdb/gdbserver/ChangeLog +++ b/gdb/gdbserver/ChangeLog @@ -1,3 +1,9 @@ +2017-09-16 Simon Marchi + + * linux-low.c (linux_qxfer_libraries_svr4): Adjust to change of + return type of xml_escape_text. + * server.c (emit_dll_description): Likewise. + 2017-09-16 Simon Marchi * server.c (captured_main): Accept argument for --selftest. diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c index 1d96ec23282..1bc8f381bc2 100644 --- a/gdb/gdbserver/linux-low.c +++ b/gdb/gdbserver/linux-low.c @@ -7288,7 +7288,6 @@ linux_qxfer_libraries_svr4 (const char *annex, unsigned char *readbuf, { /* 6x the size for xml_escape_text below. */ size_t len = 6 * strlen ((char *) libname); - char *name; if (!header_done) { @@ -7307,12 +7306,11 @@ linux_qxfer_libraries_svr4 (const char *annex, unsigned char *readbuf, p = document + document_len; } - name = xml_escape_text ((char *) libname); + std::string name = xml_escape_text ((char *) libname); p += sprintf (p, "", - name, (unsigned long) lm_addr, + name.c_str (), (unsigned long) lm_addr, (unsigned long) l_addr, (unsigned long) l_ld); - free (name); } } diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c index 99dedb2fed9..92943e2e6f3 100644 --- a/gdb/gdbserver/server.c +++ b/gdb/gdbserver/server.c @@ -1532,13 +1532,11 @@ 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; - char *name; strcpy (p, " name); - strcpy (p, name); - free (name); + std::string name = xml_escape_text (dll->name); + strcpy (p, name.c_str ()); p = p + strlen (p); strcpy (p, "\">filename); - obstack_grow_str (obstack, p); - xfree (p); + std::string p = xml_escape_text (ldi->filename); + obstack_grow_str (obstack, p.c_str ()); obstack_grow_str (obstack, "\""); if (ldi->member_name[0] != '\0') { obstack_grow_str (obstack, " member=\""); p = xml_escape_text (ldi->member_name); - obstack_grow_str (obstack, p); - xfree (p); + obstack_grow_str (obstack, p.c_str ()); obstack_grow_str (obstack, "\""); } diff --git a/gdb/unittests/xml-utils-selftests.c b/gdb/unittests/xml-utils-selftests.c index 21b506a3a0b..e4ab4830112 100644 --- a/gdb/unittests/xml-utils-selftests.c +++ b/gdb/unittests/xml-utils-selftests.c @@ -28,11 +28,9 @@ static void test_xml_escape_text () { const char *input = " &"; const char *expected_output = "<this isn't="xml"> &"; - char *actual_output = xml_escape_text (input); + std::string actual_output = xml_escape_text (input); - SELF_CHECK (strcmp (actual_output, expected_output) == 0); - - xfree (actual_output); + SELF_CHECK (actual_output == expected_output); } } diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c index 886c4ece678..197d7797d7f 100644 --- a/gdb/windows-tdep.c +++ b/gdb/windows-tdep.c @@ -371,13 +371,11 @@ void windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr, struct gdbarch *gdbarch, struct obstack *obstack) { - char *p; CORE_ADDR text_offset; obstack_grow_str (obstack, "