🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.emacs.bug
Article #98939

Patch to fix problem with regexp searches

#98939
From: Dale Worley
Date: Wed, 09 Jan 1991 21:42
128 lines
4531 bytes
Certain regular expression searches will overflow the backtracking
stack in the regular expression search routine.  Although this is
detected in the lowest level routine (re_match_2), the higher level
routines don't detect the error as a special case (re_search_2 doesn't
but is supposed to; search_buffer ignores it), and they act as if the
match attempt failed.  This can lead to unexpected failures when
searching for a pattern.  It can also lead to successes matching
in unexpected places, since if the pattern is unanchored, re_search_2
may advance the starting point and try again, eventually succeeding.

To reproduce this problem, construct a buffer containing "abcxxxdef"
in the first line, and have the following lines contain at least 2000
non-blank characters.  Put the point at the beginning of the buffer
and execute

	M-x re-search-forward RET abc[^ ]*def RET

Emacs will report that the search failed, despite that it obviously
matches.

The patch below will fix this problem by causing it to report the
error "Internal error during regexp search".  Of course, it won't make
Emacs function correctly in these cases, but it will fail blatantly
rather than mysteriously.

Dale Worley		Compass, Inc.			worley@compass.com
--
"Love is like zipping across a frozen lake in a snowmobile that
suddenly flips over, trapping you underneath.  At night, the ice
weasels come."  -- "Life in Hell"

*** regex.c.orig	Mon Jan  7 16:50:50 1991
--- regex.c	Wed Jan  9 16:00:44 1991
***************
*** 1061,1070 ****
        val = re_match_2 (pbufp, string1, size1, string2, size2, startpos, regs, mstop);
        if (0 <= val)
  	{
- 	  if (val == -2)
- 	    return -2;
  	  return startpos;
  	}
  
  #ifdef C_ALLOCA
        alloca (0);
--- 1061,1070 ----
        val = re_match_2 (pbufp, string1, size1, string2, size2, startpos, regs, mstop);
        if (0 <= val)
  	{
  	  return startpos;
  	}
+       if (val == -2)
+ 	return -2;
  
  #ifdef C_ALLOCA
        alloca (0);
*** search.c.orig	Wed Oct 25 11:23:03 1989
--- search.c	Wed Jan  9 14:43:40 1991
***************
*** 515,525 ****
  	}
        while (n < 0)
  	{
! 	  if (re_search_2 (&searchbuf, p1, s1, p2, s2,
  			   pos - FirstCharacter, lim - pos, &search_regs,
  			   /* Don't allow match past current point */
! 			   pos - FirstCharacter)
! 	      >= 0)
  	    {
  	      j = FirstCharacter;
  	      for (i = 0; i < RE_NREGS; i++)
--- 515,525 ----
  	}
        while (n < 0)
  	{
! 	  i = re_search_2 (&searchbuf, p1, s1, p2, s2,
  			   pos - FirstCharacter, lim - pos, &search_regs,
  			   /* Don't allow match past current point */
! 			   pos - FirstCharacter);
! 	  if (i >= 0)
  	    {
  	      j = FirstCharacter;
  	      for (i = 0; i < RE_NREGS; i++)
***************
*** 533,538 ****
--- 533,540 ----
  	    }
  	  else
  	    {
+ 	      if (i == -2)
+ 		  error ("Internal error during regexp search");
  	      immediate_quit = 0;
  	      return (n);
  	    }
***************
*** 540,549 ****
  	}
        while (n > 0)
  	{
! 	  if (re_search_2 (&searchbuf, p1, s1, p2, s2,
  			   pos - FirstCharacter, lim - pos, &search_regs,
! 			   lim - FirstCharacter)
! 	      >= 0)
  	    {
  	      j = FirstCharacter;
  	      for (i = 0; i < RE_NREGS; i++)
--- 542,551 ----
  	}
        while (n > 0)
  	{
! 	  i = re_search_2 (&searchbuf, p1, s1, p2, s2,
  			   pos - FirstCharacter, lim - pos, &search_regs,
! 			   lim - FirstCharacter);
! 	  if (i >= 0)
  	    {
  	      j = FirstCharacter;
  	      for (i = 0; i < RE_NREGS; i++)
***************
*** 556,561 ****
--- 558,565 ----
  	    }
  	  else
  	    {
+ 	      if (i == -2)
+ 		  error ("Internal error during regexp search");
  	      immediate_quit = 0;
  	      return (0 - n);
  	    }
Technical Details
Message-ID: <9101092142.AA28755@sn1987a.compass.com>
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!shelby!hsdndev!wuarchive!zaphod.mps.ohio-state.edu!magnus.ircc.ohio-state.edu!tut.cis.ohio-state.edu!compass.com!worley

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fb2f1,446c8d451e8de05e,start
X-Google-Attributes: gidfb2f1,public
X-Google-ArrivalTime: 1991-01-09 13:42:36 PST
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!shelby!hsdndev!wuarchive!zaphod.mps.ohio-state.edu!magnus.ircc.ohio-state.edu!tut.cis.ohio-state.edu!compass.com!worley
From: worley@compass.com (Dale Worley)
Newsgroups: gnu.emacs.bug
Subject: Patch to fix problem with regexp searches
Message-ID: <9101092142.AA28755@sn1987a.compass.com>
Date: 9 Jan 91 21:42:36 GMT
Sender: daemon@tut.cis.ohio-state.edu
Distribution: gnu
Organization: GNUs Not Usenet
Lines: 128
Approved: bug-gnu-emacs@prep.ai.mit.edu