More const use and alloca avoidance in coff-pe-read.c
authorTom Tromey <tromey@adacore.com>
Tue, 19 Apr 2022 13:41:44 +0000 (07:41 -0600)
committerTom Tromey <tromey@adacore.com>
Fri, 22 Apr 2022 12:58:42 +0000 (06:58 -0600)
This changes another function in coff-pe-read.c to use 'const' more,
and to avoid the use of alloca by instead using std::string.

gdb/coff-pe-read.c

index 5bf0e2dc7f02d01107f2c6b5f2a3747f4bd67dd9..d533a96ef6d4a85b737d945d21ef603bc4017ead 100644 (file)
@@ -525,20 +525,20 @@ read_pe_exported_syms (minimal_symbol_reader &reader,
       /* First handle forward cases.  */
       if (func_rva >= export_rva && func_rva < export_rva + export_size)
        {
-         char *forward_name = (char *) (erva + func_rva);
-         char *funcname = (char *) (erva + name_rva);
-         char *forward_dll_name = forward_name;
-         char *forward_func_name = forward_name;
-         char *sep = strrchr (forward_name, '.');
-
-         if (sep)
+         const char *forward_name = (const char *) (erva + func_rva);
+         const char *funcname = (const char *) (erva + name_rva);
+         const char *forward_dll_name = forward_name;
+         const char *forward_func_name = forward_name;
+         const char *sep = strrchr (forward_name, '.');
+
+         std::string name_storage;
+         if (sep != nullptr)
            {
              int len = (int) (sep - forward_name);
 
-             forward_dll_name = (char *) alloca (len + 1);
-             strncpy (forward_dll_name, forward_name, len);
-             forward_dll_name[len] = '\0';
-             forward_func_name = ++sep;
+             name_storage = std::string (forward_name, len);
+             forward_dll_name = name_storage.c_str ();
+             forward_func_name = sep + 1;
            }
          if (add_pe_forwarded_sym (reader, funcname, forward_dll_name,
                                    forward_func_name, ordinal,
@@ -552,7 +552,7 @@ read_pe_exported_syms (minimal_symbol_reader &reader,
          if ((func_rva >= section_data[sectix].rva_start)
              && (func_rva < section_data[sectix].rva_end))
            {
-             char *sym_name = (char *) (erva + name_rva);
+             const char *sym_name = (const char *) (erva + name_rva);
 
              section_found = 1;
              add_pe_exported_sym (reader, sym_name, func_rva, ordinal,
@@ -563,7 +563,7 @@ read_pe_exported_syms (minimal_symbol_reader &reader,
        }
       if (!section_found)
        {
-         char *funcname = (char *) (erva + name_rva);
+         const char *funcname = (const char *) (erva + name_rva);
 
          if (name_rva == 0)
            {