develooper Front page | perl.perl5.porters | Postings from March 2006

[perl #1760] regexp causes SIGSEGV (stack overflow?)

From:
Steve Peters via RT
Date:
March 28, 2006 19:24
Subject:
[perl #1760] regexp causes SIGSEGV (stack overflow?)
Message ID:
rt-3.0.11-1760-131590.3.13081281407001@perl.org
> [amc@arwen.cs.berkeley.edu - Tue Nov 09 12:08:10 1999]:
> 
> 
> -----------------------------------------------------------------
> [Please enter your report here]
> 
> The following program demonstrates a regular expression match on
> a long string causing a SIGSEGV:
> 
> #!/usr/bin/perl
> $s = 'x';
> for ($i = 1;  $i < 16; ++$i) {
>   $s = $s . $s;
>   print length($s), "\n";
>   $s =~ /(a?x)*/;
> }
> 
> After the length of $s reaches 32768, perl crashes.  I tried getting a
> stack trace from gdb, but the stack appeared to be extremely large, so
> maybe the problem is that the stack is overflowing.  Should that cause
> a
> SIGSEGV, or should it be handled more gracefully?
> 
> Is in necessary for that regular expression to require stack space
> proportional to the size of the matched string?  (It's been too long
> since I took a compilers class.)
> 
> By the way, the regular expression is a greatly simplified version of
> one I was actually using to remove a prefix from a string and print
> it.
> The original code was structured like this:
> 
>     if ($s =~ s/^((a?x)+)//) { print $1 }
> 
> My workaround is structured like this:
> 
>     while ($s =~ s/^((a?x){1,999})//) { print $1 }
> 

It appears that as of change #27598, this no longer core dumps.  davem++ :)




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About