develooper Front page | perl.perl5.porters | Postings from December 2000

[ID 19991109.005] regexp causes SIGSEGV (stack overflow?)

From:
Stephen P. Potter
Date:
December 13, 2000 08:39
Subject:
[ID 19991109.005] regexp causes SIGSEGV (stack overflow?)
Message ID:
200012131641.LAA05428@spotter.yi.org
still a problem as of 8101 on both Mandrake 7.2 and Solaris 8.0

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 }




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