This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] PR fortran/14129: Don't xrealloc a local array


The following patch is my proposed solution to PR fortran/14129 which
is an ICE-on-valid targetted for 3.4 that's triggered by compiling a
.F file with a long absolute path name.  The cause is a bug in the
function ffelex_cfelex_ that's responsible for parsing arbitrary length
tokens.  This code mistakenly calls xrealloc on a local (automatic)
array that's allocated on the stack.

The fix below is to check each time the buffer fills, if this is the
first resize, and therefore call xmalloc and memcpy instead of realloc.
The test "q == &buff[0]" is the same condition used in the function
to determine whether to call "free" or not.

The solution proposed in the bugzilla PR of increasing the size of the
local array, i.e. the inital size of the buffer, just postpones the
inevitable "segmentation fault" and uses more memory in the typical
case.

The following patch has been tested on i686-pc-linux-gnu with a full
"make bootstrap" with --enable-languages="c,f77", and a "make -k check"
with no new failures.  The testcase is trivial, but would require
support for preprocessed files somewhere in the fortran testsuite.

Ok for mainline and 3.4?


2004-02-14  Roger Sayle  <roger@eyesopen.com>

	PR fortran/14129
	* lex.c (ffelex_cfelex_): Avoid calling xrealloc on a local stack
	allocated array.


Index: lex.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/f/lex.c,v
retrieving revision 1.45
diff -c -3 -p -r1.45 lex.c
*** lex.c	21 Oct 2003 14:40:11 -0000	1.45
--- lex.c	14 Feb 2004 15:54:45 -0000
*************** ffelex_cfelex_ (ffelexToken *xtoken, FIL
*** 694,700 ****
  	      register unsigned bytes_used = (p - q);

  	      buffer_length *= 2;
! 	      q = xrealloc (q, buffer_length);
  	      p = &q[bytes_used];
  	      r = &q[buffer_length];
  	    }
--- 694,706 ----
  	      register unsigned bytes_used = (p - q);

  	      buffer_length *= 2;
! 	      if (q == &buff[0])
! 		{
! 		  q = xmalloc (buffer_length);
! 		  memcpy (q, buff, bytes_used);
! 		}
! 	      else
! 		q = xrealloc (q, buffer_length);
  	      p = &q[bytes_used];
  	      r = &q[buffer_length];
  	    }
*************** ffelex_cfelex_ (ffelexToken *xtoken, FIL
*** 754,760 ****
  		  register unsigned bytes_used = (p - q);

  		  buffer_length = bytes_used * 2;
! 		  q = xrealloc (q, buffer_length);
  		  p = &q[bytes_used];
  		  r = &q[buffer_length];
  		}
--- 760,772 ----
  		  register unsigned bytes_used = (p - q);

  		  buffer_length = bytes_used * 2;
! 		  if (q == &buff[0])
! 		    {
! 		      q = xmalloc (buffer_length);
! 		      memcpy (q, buff, bytes_used);
! 		    }
! 		  else
! 		    q = xrealloc (q, buffer_length);
  		  p = &q[bytes_used];
  		  r = &q[buffer_length];
  		}

Roger
--
Roger Sayle,                         E-mail: roger@eyesopen.com
OpenEye Scientific Software,         WWW: http://www.eyesopen.com/
Suite 1107, 3600 Cerrillos Road,     Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507.         Fax: (+1) 505-473-0833


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]