From 9c6c44713f31f7b27bfe6921de378fa69127a048 Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Sat, 19 Feb 2022 13:08:32 +0000 Subject: [PATCH] gdb: make use of std::string in dbxread.c and xcoffread.c While taking a look through dbxread.c I spotted a couple of places where making use of std::string would remove the need for manual memory allocation and memcpy. During review Simon pointed out that the same code exists in xcoffread.c, so I've applied the same fix there too. There should be no user visible changes after this commit. --- gdb/dbxread.c | 18 ++++-------------- gdb/xcoffread.c | 18 ++++-------------- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/gdb/dbxread.c b/gdb/dbxread.c index 911d518b7e9..ddda5df4ee1 100644 --- a/gdb/dbxread.c +++ b/gdb/dbxread.c @@ -1603,13 +1603,8 @@ read_dbx_symtab (minimal_symbol_reader &reader, case 'f': if (! pst) { - int name_len = p - namestring; - char *name = (char *) xmalloc (name_len + 1); - - memcpy (name, namestring, name_len); - name[name_len] = '\0'; - function_outside_compilation_unit_complaint (name); - xfree (name); + std::string name (namestring, (p - namestring)); + function_outside_compilation_unit_complaint (name.c_str ()); } /* Kludges for ELF/STABS with Sun ACC. */ last_function_name = namestring; @@ -1663,13 +1658,8 @@ read_dbx_symtab (minimal_symbol_reader &reader, case 'F': if (! pst) { - int name_len = p - namestring; - char *name = (char *) xmalloc (name_len + 1); - - memcpy (name, namestring, name_len); - name[name_len] = '\0'; - function_outside_compilation_unit_complaint (name); - xfree (name); + std::string name (namestring, (p - namestring)); + function_outside_compilation_unit_complaint (name.c_str ()); } /* Kludges for ELF/STABS with Sun ACC. */ last_function_name = namestring; diff --git a/gdb/xcoffread.c b/gdb/xcoffread.c index d3e9ade72cb..f6db7f9ff7e 100644 --- a/gdb/xcoffread.c +++ b/gdb/xcoffread.c @@ -2734,13 +2734,8 @@ scan_xcoff_symtab (minimal_symbol_reader &reader, case 'f': if (! pst) { - int name_len = p - namestring; - char *name = (char *) xmalloc (name_len + 1); - - memcpy (name, namestring, name_len); - name[name_len] = '\0'; - function_outside_compilation_unit_complaint (name); - xfree (name); + std::string name (namestring, (p - namestring)); + function_outside_compilation_unit_complaint (name.c_str ()); } pst->add_psymbol (gdb::string_view (namestring, p - namestring), @@ -2758,13 +2753,8 @@ scan_xcoff_symtab (minimal_symbol_reader &reader, case 'F': if (! pst) { - int name_len = p - namestring; - char *name = (char *) xmalloc (name_len + 1); - - memcpy (name, namestring, name_len); - name[name_len] = '\0'; - function_outside_compilation_unit_complaint (name); - xfree (name); + std::string name (namestring, (p - namestring)); + function_outside_compilation_unit_complaint (name.c_str ()); } /* We need only the minimal symbols for these -- 2.30.2