[comp.lang.perl] perly.fixer fixes

tdinger@hiredgun.East.Sun.COM (Tom Dinger - Sun BOS SPA) (03/23/91)

	"Fix" perly.fixer for SunOS yacc, and add memory exhaustion test
	For Perl 4.000
	22 March 1991
	Tom Dinger (tdinger@East.SUN.COM)

	The perly.fixer as distributed "almost" works with the YACC output
	on SunOS (at least one version: 4.0.2 on the Sun386i).  The overflow
	comparison as checked in perly.fixer looks for a "buggy" version of
	the comparison: it check for ">" when the correct comparison is ">=".
	This patch fixes the comparison check, by matching either version.

	In addition, this patch adds out-of-memory checking to the malloc()
	and realloc() calls; unlikely to occurr for UNIX, but much more likely
	for limited-memory architectures, like DOS.  Incidentally, it seems
	that in SunOS 4.1 (and later), the YACC parser already uses this
	same dynamic allocation scheme, and it _has_ the out-of-memory test
	in it.

	Apply with the patch program ("patch -p -N < thisfile").

*** orig/perly.fixer	Fri Mar 22 20:32:12 1991
--- perly.fixer	Fri Mar 22 20:32:52 1991
***************
*** 8,14 ****
  short[ 	]*yys\[ *YYMAXDEPTH *\] *;
  yyps *= *&yys\[ *-1 *\];
  yypv *= *&yyv\[ *-1 *\];
! if *\( *\+\+yyps *> *&yys\[ *YYMAXDEPTH *\] *\)' $input >$tmp
  set `wc -l $tmp`
  
  case "$1" in
--- 8,14 ----
  short[ 	]*yys\[ *YYMAXDEPTH *\] *;
  yyps *= *&yys\[ *-1 *\];
  yypv *= *&yyv\[ *-1 *\];
! if *\( *\+\+yyps *>=* *&yys\[ *YYMAXDEPTH *\] *\)' $input >$tmp
  set `wc -l $tmp`
  
  case "$1" in
***************
*** 31,36 ****
--- 31,40 ----
  \	if (!yyv) {\
  \	    yyv = (YYSTYPE*) malloc(yymaxdepth * sizeof(YYSTYPE));\
  \	    yys = (short*) malloc(yymaxdepth * sizeof(short));\
+ \	    if ( ! yyv || ! yys ) {\
+ \		yyerror( "out of memory" );\
+ \		return(1);\
+ \	    }\
  \	    maxyyps = &yys[yymaxdepth];\
  \	}\
  \	yyps = &yys[-1];\
***************
*** 37,52 ****
  \	yypv = &yyv[-1];
  
  
! /if *( *\+\+yyps *> *&yys\[ *YYMAXDEPTH *\] *)/c\
  \		if( ++yyps >= maxyyps ) {\
  \		    int tv = yypv - yyv;\
  \		    int ts = yyps - yys;\
  \
  \		    yymaxdepth *= 2;\
! \		    yyv = (YYSTYPE*)realloc((char*)yyv,\
  \		      yymaxdepth*sizeof(YYSTYPE));\
! \		    yys = (short*)realloc((char*)yys,\
  \		      yymaxdepth*sizeof(short));\
  \		    yyps = yys + ts;\
  \		    yypv = yyv + tv;\
  \		    maxyyps = &yys[yymaxdepth];\
--- 41,64 ----
  \	yypv = &yyv[-1];
  
  
! /if *( *\+\+yyps *>=* *&yys\[ *YYMAXDEPTH *\] *)/c\
  \		if( ++yyps >= maxyyps ) {\
+ \		    YYSTYPE * temp_yyv;\
+ \		    short * temp_yys;\
  \		    int tv = yypv - yyv;\
  \		    int ts = yyps - yys;\
  \
  \		    yymaxdepth *= 2;\
! \		    temp_yyv = (YYSTYPE*)realloc((char*)yyv,\
  \		      yymaxdepth*sizeof(YYSTYPE));\
! \		    temp_yys = (short*)realloc((char*)yys,\
  \		      yymaxdepth*sizeof(short));\
+ \		    if ( ! temp_yyv || ! temp_yys ) {\
+ \			yyerror( "yacc stack overflow" );\
+ \			return(1);\
+ \		    }\
+ \		    yyv = temp_yyv;\
+ \		    yys = temp_yys;\
  \		    yyps = yys + ts;\
  \		    yypv = yyv + tv;\
  \		    maxyyps = &yys[yymaxdepth];\
Tom Dinger	     consulting at:
TechnoLogics, Inc.        Sun Microsystems    Internet: tdinger@East.Sun.COM
(508)486-8500             (508)671-0521       UUCP: ...!sun!suneast!tdinger