PR27716, build failure for msdosdjgpp: PATH_MAX undeclared
authorAlan Modra <amodra@gmail.com>
Wed, 14 Apr 2021 03:12:27 +0000 (12:42 +0930)
committerAlan Modra <amodra@gmail.com>
Wed, 14 Apr 2021 05:36:11 +0000 (15:06 +0930)
We shouldn't be using arbitrary limits like PATH_MAX in GNU programs.
This patch also fixes some memory leaks in readelf when processing
separate debug info.

PR 27716
binutils/
* objdump.c (show_line): Don't limit paths to PATH_MAX.
* readelf.c (struct filedata): Change program_interpreter from
a char array to a char pointer.
(process_program_headers): Sanity check PT_INTERP p_filesz.
Malloc program_interpreter using p_filesz and read directly from
file.
(process_dynamic_section): Check program_interpreter is non-NULL.
(free_filedata): New function, split out from..
(process_object): ..here.
(close_debug_file): Call free_filedata.
* sysdep.h: Don't include sys/param.h.
(PATH_MAX): Don't define.
* configure.ac: Don't check for sys/param.h.
* configure: Regenerate.
gprof/
* gprof.h (PATH_MAX): Don't define.
* corefile.c (core_create_line_syms): Don't use PATH_MAX for initial
file name size.
* source.c (annotate_source): Malloc file name buffer.  Always
trim off "-ann" when dos 8.3 annotate file matches original.
* utils.c (print_name_only): Malloc file name buffer.

binutils/ChangeLog
binutils/configure
binutils/configure.ac
binutils/objdump.c
binutils/readelf.c
binutils/sysdep.h
gprof/ChangeLog
gprof/corefile.c
gprof/gprof.h
gprof/source.c
gprof/utils.c

index ef0e868e1a01e593b20bfb4ca151b9a99b20cd7d..5148da4a2197185c5a4df88451d18cbb0825ebb3 100644 (file)
@@ -1,3 +1,21 @@
+2021-04-14  Alan Modra  <amodra@gmail.com>
+
+       PR 27716
+       * objdump.c (show_line): Don't limit paths to PATH_MAX.
+       * readelf.c (struct filedata): Change program_interpreter from
+       a char array to a char pointer.
+       (process_program_headers): Sanity check PT_INTERP p_filesz.
+       Malloc program_interpreter using p_filesz and read directly from
+       file.
+       (process_dynamic_section): Check program_interpreter is non-NULL.
+       (free_filedata): New function, split out from..
+       (process_object): ..here.
+       (close_debug_file): Call free_filedata.
+       * sysdep.h: Don't include sys/param.h.
+       (PATH_MAX): Don't define.
+       * configure.ac: Don't check for sys/param.h.
+       * configure: Regenerate.
+
 2021-04-13  Frederic Cambus  <fred@statdns.com>
 
        * readelf.c (process_netbsd_elf_note): Remove now unneeded #ifdef
index 938ef4836989fcc5c2204165246e242a31caec03..82720324e0db12679178e5d627171ef2bcb59f0d 100755 (executable)
@@ -12983,7 +12983,7 @@ _ACEOF
 # guarantees they are available.
 # plugin-api.h tests HAVE_STDINT_H and HAVE_INTTYPES_H
 # Besides those, we need to check anything used in binutils/ not in C99.
-for ac_header in fcntl.h inttypes.h stdint.h sys/file.h sys/param.h \
+for ac_header in fcntl.h inttypes.h stdint.h sys/file.h \
                 sys/stat.h sys/types.h unistd.h
 do :
   as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
index 2553b809e6c1f26a1843d8ad3dd435af61587c2d..3c5a8e13da3ca88474050dd66f4c20540e2fd494 100644 (file)
@@ -181,7 +181,7 @@ AC_CHECK_SIZEOF([long long])
 # guarantees they are available.
 # plugin-api.h tests HAVE_STDINT_H and HAVE_INTTYPES_H
 # Besides those, we need to check anything used in binutils/ not in C99.
-AC_CHECK_HEADERS(fcntl.h inttypes.h stdint.h sys/file.h sys/param.h \
+AC_CHECK_HEADERS(fcntl.h inttypes.h stdint.h sys/file.h \
                 sys/stat.h sys/types.h unistd.h)
 AC_HEADER_SYS_WAIT
 AC_FUNC_MMAP
index 3e6bf721e12fda096d0b115790318b3029935b47..39b5793bc2e13c11fc345d1db42fb738c11582b9 100644 (file)
@@ -1739,7 +1739,7 @@ show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
       char *path_up;
       const char *fname = filename;
 
-      path = xmalloc (prefix_length + PATH_MAX + 1);
+      path = xmalloc (prefix_length + 1 + strlen (filename));
 
       if (prefix_length)
        memcpy (path, prefix, prefix_length);
@@ -1762,8 +1762,7 @@ show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
        }
 
       /* Update complete filename.  */
-      strncpy (path_up, fname, PATH_MAX);
-      path_up[PATH_MAX] = '\0';
+      strcpy (path_up, fname);
 
       filename = path;
       reloc = true;
index 9d0104d979172b4fba7d041d71f1eb28d5fe237c..dc7764a9bb9fbeca7dee70ecad19141a35553f90 100644 (file)
@@ -292,7 +292,7 @@ typedef struct filedata
   bfd_vma *            gnuchains;
   bfd_vma *            mipsxlat;
   bfd_vma              gnusymidx;
-  char                 program_interpreter[PATH_MAX];
+  char *               program_interpreter;
   bfd_vma              dynamic_info[DT_ENCODING];
   bfd_vma              dynamic_info_DT_GNU_HASH;
   bfd_vma              dynamic_info_DT_MIPS_XHASH;
@@ -5538,22 +5538,21 @@ the .dynamic section is not the same as the dynamic segment\n"));
          break;
 
        case PT_INTERP:
-         if (fseek (filedata->handle,
-                    filedata->archive_file_offset + (long) segment->p_offset,
-                    SEEK_SET))
+         if (segment->p_offset >= filedata->file_size
+             || segment->p_filesz > filedata->file_size - segment->p_offset
+             || segment->p_filesz - 1 >= (size_t) -2
+             || fseek (filedata->handle,
+                       filedata->archive_file_offset + (long) segment->p_offset,
+                       SEEK_SET))
            error (_("Unable to find program interpreter name\n"));
          else
            {
-             char fmt [32];
-             int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX - 1);
-
-             if (ret >= (int) sizeof (fmt) || ret < 0)
-               error (_("Internal error: failed to create format string to display program interpreter\n"));
-
-             filedata->program_interpreter[0] = 0;
-             if (fscanf (filedata->handle, fmt,
-                         filedata->program_interpreter) <= 0)
-               error (_("Unable to read program interpreter name\n"));
+             size_t len = segment->p_filesz;
+             free (filedata->program_interpreter);
+             filedata->program_interpreter = xmalloc (len + 1);
+             len = fread (filedata->program_interpreter, 1, len,
+                          filedata->handle);
+             filedata->program_interpreter[len] = 0;
 
              if (do_segments)
                printf (_("      [Requesting program interpreter: %s]\n"),
@@ -11094,7 +11093,8 @@ the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
                    case DT_NEEDED:
                      printf (_("Shared library: [%s]"), name);
 
-                     if (streq (name, filedata->program_interpreter))
+                     if (filedata->program_interpreter
+                         && streq (name, filedata->program_interpreter))
                        printf (_(" program interpreter"));
                      break;
 
@@ -21050,6 +21050,70 @@ get_file_header (Filedata * filedata)
   return true;
 }
 
+static void
+free_filedata (Filedata *filedata)
+{
+  free (filedata->program_interpreter);
+  filedata->program_interpreter = NULL;
+
+  free (filedata->program_headers);
+  filedata->program_headers = NULL;
+
+  free (filedata->section_headers);
+  filedata->section_headers = NULL;
+
+  free (filedata->string_table);
+  filedata->string_table = NULL;
+  filedata->string_table_length = 0;
+
+  free (filedata->dump.dump_sects);
+  filedata->dump.dump_sects = NULL;
+  filedata->dump.num_dump_sects = 0;
+
+  free (filedata->dynamic_strings);
+  filedata->dynamic_strings = NULL;
+  filedata->dynamic_strings_length = 0;
+
+  free (filedata->dynamic_symbols);
+  filedata->dynamic_symbols = NULL;
+  filedata->num_dynamic_syms = 0;
+
+  free (filedata->dynamic_syminfo);
+  filedata->dynamic_syminfo = NULL;
+
+  free (filedata->dynamic_section);
+  filedata->dynamic_section = NULL;
+
+  while (filedata->symtab_shndx_list != NULL)
+    {
+      elf_section_list *next = filedata->symtab_shndx_list->next;
+      free (filedata->symtab_shndx_list);
+      filedata->symtab_shndx_list = next;
+    }
+
+  free (filedata->section_headers_groups);
+  filedata->section_headers_groups = NULL;
+
+  if (filedata->section_groups)
+    {
+      size_t i;
+      struct group_list * g;
+      struct group_list * next;
+
+      for (i = 0; i < filedata->group_count; i++)
+       {
+         for (g = filedata->section_groups [i].root; g != NULL; g = next)
+           {
+             next = g->next;
+             free (g);
+           }
+       }
+
+      free (filedata->section_groups);
+      filedata->section_groups = NULL;
+    }
+}
+
 static void
 close_file (Filedata * filedata)
 {
@@ -21064,6 +21128,7 @@ close_file (Filedata * filedata)
 void
 close_debug_file (void * data)
 {
+  free_filedata ((Filedata *) data);
   close_file ((Filedata *) data);
 }
 
@@ -21277,61 +21342,7 @@ process_object (Filedata * filedata)
   if (! process_arch_specific (filedata))
     res = false;
 
-  free (filedata->program_headers);
-  filedata->program_headers = NULL;
-
-  free (filedata->section_headers);
-  filedata->section_headers = NULL;
-
-  free (filedata->string_table);
-  filedata->string_table = NULL;
-  filedata->string_table_length = 0;
-
-  free (filedata->dump.dump_sects);
-  filedata->dump.dump_sects = NULL;
-  filedata->dump.num_dump_sects = 0;
-
-  free (filedata->dynamic_strings);
-  filedata->dynamic_strings = NULL;
-  filedata->dynamic_strings_length = 0;
-
-  free (filedata->dynamic_symbols);
-  filedata->dynamic_symbols = NULL;
-  filedata->num_dynamic_syms = 0;
-
-  free (filedata->dynamic_syminfo);
-  filedata->dynamic_syminfo = NULL;
-
-  free (filedata->dynamic_section);
-  filedata->dynamic_section = NULL;
-
-  while (filedata->symtab_shndx_list != NULL)
-    {
-      elf_section_list *next = filedata->symtab_shndx_list->next;
-      free (filedata->symtab_shndx_list);
-      filedata->symtab_shndx_list = next;
-    }
-
-  free (filedata->section_headers_groups);
-  filedata->section_headers_groups = NULL;
-
-  if (filedata->section_groups)
-    {
-      struct group_list * g;
-      struct group_list * next;
-
-      for (i = 0; i < filedata->group_count; i++)
-       {
-         for (g = filedata->section_groups [i].root; g != NULL; g = next)
-           {
-             next = g->next;
-             free (g);
-           }
-       }
-
-      free (filedata->section_groups);
-      filedata->section_groups = NULL;
-    }
+  free_filedata (filedata);
 
   free_debug_memory ();
 
index 747ff4c31ffae68e70ac52c2fc2af238db684794..16601e5d22e5333e012f04e7f376fe67c74acaab 100644 (file)
@@ -124,23 +124,8 @@ extern char **environ;
 /* Used by ar.c and objcopy.c.  */
 #define BUFSIZE 8192
 
-/* For PATH_MAX.  */
 #include <limits.h>
 
-#ifndef PATH_MAX
-/* For MAXPATHLEN.  */
-# ifdef HAVE_SYS_PARAM_H
-#  include <sys/param.h>
-# endif
-# ifndef PATH_MAX
-#  ifdef MAXPATHLEN
-#   define PATH_MAX MAXPATHLEN
-#  else
-#   define PATH_MAX 1024
-#  endif
-# endif
-#endif
-
 #if SIZEOF_LONG_LONG > SIZEOF_LONG
 /* We can't use any bfd types here since readelf may define BFD64 and
    objdump may not.  */
index c1169011781047cd69d040d1c50c161d0fafc84b..6e97b43b20f6e105e859eb3c086c370d9aba557b 100644 (file)
@@ -1,3 +1,13 @@
+2021-04-14  Alan Modra  <amodra@gmail.com>
+
+       PR 27716
+       * gprof.h (PATH_MAX): Don't define.
+       * corefile.c (core_create_line_syms): Don't use PATH_MAX for initial
+       file name size.
+       * source.c (annotate_source): Malloc file name buffer.  Always
+       trim off "-ann" when dos 8.3 annotate file matches original.
+       * utils.c (print_name_only): Malloc file name buffer.
+
 2021-04-05  Alan Modra  <amodra@gmail.com>
 
        * configure.ac: Check for sys/time.h and setitimer.  Don't invoke
index 831d589f29c85d45072324bcd46df9e983c31607..b5e716dcbf427e244162b06c0374d1e39c9b8333 100644 (file)
@@ -781,8 +781,8 @@ core_create_line_syms (void)
 
      Of course, this is rather slow and it would be better if
      BFD would provide an iterator for enumerating all line infos.  */
-  prev_name_len = PATH_MAX;
-  prev_filename_len = PATH_MAX;
+  prev_name_len = 1024;
+  prev_filename_len = 1024;
   prev_name = (char *) xmalloc (prev_name_len);
   prev_filename = (char *) xmalloc (prev_filename_len);
   ltab.len = 0;
index 1c23dd83f2f4fc1f5833c3dda7fc7528cb1d3176..1d8d89647727c07584a5e9cd5b9c724ab9ce1c73 100644 (file)
 /* AIX defines hz as a macro.  */
 #undef hz
 
-#ifndef PATH_MAX
-#define PATH_MAX       1024
-#endif
-
 #define        A_OUTNAME       "a.out"         /* default core filename */
 #define        GMONNAME        "gmon.out"      /* default profile filename */
 #define        GMONSUM         "gmon.sum"      /* profile summary filename */
index e648a3a7465d571d316f15cbe5fc0c98984c735f..648276bf37677f2dce1147d6e70323ea64dda400 100644 (file)
@@ -97,14 +97,14 @@ annotate_source (Source_File *sf, unsigned int max_width,
   int i, line_num, nread;
   bool new_line;
   char buf[8192];
-  char fname[PATH_MAX];
+  char *fname;
   char *annotation, *name_only;
   FILE *ifp, *ofp;
   Search_List_Elem *sle = src_search_list.head;
 
   /* Open input file.  If open fails, walk along search-list until
      open succeeds or reaching end of list.  */
-  strcpy (fname, sf->name);
+  fname = (char *) sf->name;
 
   if (IS_ABSOLUTE_PATH (sf->name))
     sle = 0;                   /* Don't use search list for absolute paths.  */
@@ -116,6 +116,8 @@ annotate_source (Source_File *sf, unsigned int max_width,
                             sf->name, fname));
 
       ifp = fopen (fname, FOPEN_RB);
+      if (fname != sf->name)
+       free (fname);
       if (ifp)
        break;
 
@@ -141,6 +143,8 @@ annotate_source (Source_File *sf, unsigned int max_width,
 
       if (sle)
        {
+         fname = xmalloc (strlen (sle->path) + 3
+                          + strlen (name_only ? name_only : sf->name));
          strcpy (fname, sle->path);
 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
          /* d:foo is not the same thing as d:/foo!  */
@@ -191,6 +195,7 @@ annotate_source (Source_File *sf, unsigned int max_width,
       else
        filename = sf->name;
 
+      fname = xmalloc (strlen (filename) + strlen (EXT_ANNO) + 1);
       strcpy (fname, filename);
       strcat (fname, EXT_ANNO);
 #ifdef __MSDOS__
@@ -205,9 +210,9 @@ annotate_source (Source_File *sf, unsigned int max_width,
          {
            char *dot = strrchr (fname, '.');
 
-           if (dot)
-             *dot = '\0';
-           strcat (fname, ".ann");
+           if (!dot)
+             dot = fname + strlen (filename);
+           strcpy (dot, ".ann");
          }
       }
 #endif
@@ -216,8 +221,10 @@ annotate_source (Source_File *sf, unsigned int max_width,
       if (!ofp)
        {
          perror (fname);
+         free (fname);
          return 0;
        }
+      free (fname);
     }
 
   /* Print file names if output goes to stdout
index 76bc57d31831994d696795b58cf5fb8e1cce51b3..2c0489540dc16c0461f04543e977dead3a31ed83 100644 (file)
@@ -43,9 +43,7 @@ int
 print_name_only (Sym *self)
 {
   const char *name = self->name;
-  const char *filename;
   char *demangled = 0;
-  char buf[PATH_MAX];
   int size = 0;
 
   if (name)
@@ -60,7 +58,9 @@ print_name_only (Sym *self)
       size = strlen (name);
       if ((line_granularity || inline_file_names) && self->file)
        {
-         filename = self->file->name;
+         const char *filename = self->file->name;
+         char *buf;
+
          if (!print_path)
            {
              filename = strrchr (filename, '/');
@@ -73,6 +73,7 @@ print_name_only (Sym *self)
                  filename = self->file->name;
                }
            }
+         buf = xmalloc (strlen (filename) + 8 + 20 + 16);
          if (line_granularity)
            {
              sprintf (buf, " (%s:%d @ %lx)", filename, self->line_num,
@@ -84,6 +85,7 @@ print_name_only (Sym *self)
            }
          printf ("%s", buf);
          size += strlen (buf);
+         free (buf);
        }
       free (demangled);
       DBG (DFNDEBUG, printf ("{%d} ", self->cg.top_order));
This page took 0.043072 seconds and 4 git commands to generate.