[Bug fortran/23065] MAXPATHLEN usage in fortran/{scanner,module}.c

Steve Kargl sgk@troutmask.apl.washington.edu
Mon Aug 1 18:26:00 GMT 2005


On Mon, Aug 01, 2005 at 04:46:17PM -0000, pinskia at gcc dot gnu dot org wrote:
> 
> Well in both the cases in the fortran front-end, really an alloca
> should be used instead of MAXPATHLEN.
> 

The attached patches uses alloca to remove the use of PATH_MAX
from gfortran.  It also fixes one other nearby hardcoded buffer.

This has been bubblestrapped and regression tested on amd64-*-freebsd.

2005-08-01  Steven G. Kargl  <kargls@comcast.net>

	PR fortran/23065
	* gfortran.h: Remove PATH_MAX definition.
	* module.c (write_module,gfc_dump_module): Use alloca to allocate buffers.
	* scanner.s (gfc_release_include_path,form_from_filename): Ditto.

-- 
Steve
-------------- next part --------------
Index: gfortran.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fortran/gfortran.h,v
retrieving revision 1.76
diff -c -p -r1.76 gfortran.h
*** gfortran.h	14 Jul 2005 10:12:16 -0000	1.76
--- gfortran.h	1 Aug 2005 18:16:47 -0000
*************** typedef struct 
*** 509,521 ****
  #endif
  
  
- #include <limits.h>
- #ifndef PATH_MAX
- # include <sys/param.h>
- # define PATH_MAX MAXPATHLEN
- #endif
- 
- 
  extern int gfc_suppress_error;
  
  
--- 509,514 ----
Index: module.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fortran/module.c,v
retrieving revision 1.34
diff -c -p -r1.34 module.c
*** module.c	25 Jun 2005 00:40:35 -0000	1.34
--- module.c	1 Aug 2005 18:16:47 -0000
*************** write_module (void)
*** 3479,3492 ****
  void
  gfc_dump_module (const char *name, int dump_flag)
  {
!   char filename[PATH_MAX], *p;
    time_t now;
  
!   filename[0] = '\0';
    if (gfc_option.module_dir != NULL)
!     strcpy (filename, gfc_option.module_dir);
! 
!   strcat (filename, name);
    strcat (filename, MODULE_EXTENSION);
  
    if (!dump_flag)
--- 3479,3500 ----
  void
  gfc_dump_module (const char *name, int dump_flag)
  {
!   int n;
!   char *filename, *p;
    time_t now;
  
!   n = strlen (name) + strlen (MODULE_EXTENSION) + 1;
    if (gfc_option.module_dir != NULL)
!     {
!       filename = (char *) alloca (n + strlen (gfc_option.module_dir));
!       strcpy (filename, gfc_option.module_dir);
!       strcat (filename, name);
!     }
!   else
!     {
!       filename = (char *) alloca (n);
!       strcpy (filename, name);
!     }
    strcat (filename, MODULE_EXTENSION);
  
    if (!dump_flag)
*************** gfc_dump_module (const char *name, int d
*** 3532,3541 ****
  void
  gfc_use_module (void)
  {
!   char filename[GFC_MAX_SYMBOL_LEN + 5];
    gfc_state_data *p;
    int c, line;
  
    strcpy (filename, module_name);
    strcat (filename, MODULE_EXTENSION);
  
--- 3540,3551 ----
  void
  gfc_use_module (void)
  {
!   char *filename;
    gfc_state_data *p;
    int c, line;
  
+   filename = (char *) alloca(strlen(module_name) + strlen(MODULE_EXTENSION)
+ 			     + 1);
    strcpy (filename, module_name);
    strcat (filename, MODULE_EXTENSION);
  
Index: scanner.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fortran/scanner.c,v
retrieving revision 1.22
diff -c -p -r1.22 scanner.c
*** scanner.c	14 Jul 2005 07:14:37 -0000	1.22
--- scanner.c	1 Aug 2005 18:16:47 -0000
*************** gfc_release_include_path (void)
*** 164,170 ****
  FILE *
  gfc_open_included_file (const char *name)
  {
!   char fullname[PATH_MAX];
    gfc_directorylist *p;
    FILE *f;
  
--- 164,170 ----
  FILE *
  gfc_open_included_file (const char *name)
  {
!   char *fullname;
    gfc_directorylist *p;
    FILE *f;
  
*************** gfc_open_included_file (const char *name
*** 174,182 ****
  
    for (p = include_dirs; p; p = p->next)
      {
!       if (strlen (p->path) + strlen (name) + 1 > PATH_MAX)
! 	continue;
! 
        strcpy (fullname, p->path);
        strcat (fullname, name);
  
--- 174,180 ----
  
    for (p = include_dirs; p; p = p->next)
      {
!       fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
        strcpy (fullname, p->path);
        strcat (fullname, name);
  
*************** form_from_filename (const char *filename
*** 1133,1147 ****
    const char *fileext;
    int i;
  
!   /* Find end of file name.  */
    i = 0;
!   while ((i < PATH_MAX) && (filename[i] != '\0'))
      i++;
  
-   /* Improperly terminated or too-long filename.  */
-   if (i == PATH_MAX)
-     return FORM_UNKNOWN;
- 
    /* Find last period.  */
    while (i >= 0 && (filename[i] != '.'))
      i--;
--- 1131,1142 ----
    const char *fileext;
    int i;
  
!   /* Find end of file name.  Note, filename is either a NULL pointer or
!      a NUL terminated string.  */
    i = 0;
!   while (filename[i] != '\0')
      i++;
  
    /* Find last period.  */
    while (i >= 0 && (filename[i] != '.'))
      i--;


More information about the Fortran mailing list