Bug 29047 - StackOverflowError by gnu.java.util.regex.RETokenRepeated
Summary: StackOverflowError by gnu.java.util.regex.RETokenRepeated
Status: RESOLVED FIXED
Alias: None
Product: classpath
Classification: Unclassified
Component: classpath (show other bugs)
Version: 0.93
: P3 normal
Target Milestone: ---
Assignee: Ito Kazumitsu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-09-12 22:10 UTC by Elias Naur
Modified: 2006-09-23 07:43 UTC (History)
1 user (show)

See Also:
Host: i686-suse-linux
Target: i686-suse-linux
Build: i686-suse-linux
Known to work:
Known to fail:
Last reconfirmed: 2006-09-18 22:30:43


Attachments
test program (709 bytes, application/x-tgz)
2006-09-12 22:11 UTC, Elias Naur
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Elias Naur 2006-09-12 22:10:22 UTC
I get a StackOverflowError when trying to run the test at:

http://download.oddlabs.com/elias/classpathxml.tar.gz

The test is a larger program pruned as much as possible while still demonstrating the stack overflow.

Steps to reproduce:
1. Use Classpath CVS HEAD
2. Use either JamVM 1.4.3 or the Jikes RVM from SVN
3. Run the run.sh script from the test.

I've noticed that turning off validation (the line containing factory.setValidating(true)) the test will run successfully. It seems that the validator stack depth is dependent on the number of child nodes (the <b/> nodes in this case), which is unfortunate.
Comment 1 Elias Naur 2006-09-12 22:11:28 UTC
Created attachment 12236 [details]
test program
Comment 2 chris burdess 2006-09-16 18:09:10 UTC
The stack overflow appears to be occurring in the regex code rather than anything inherent in the deisgn of the validation code. Ito, perhaps you could have a look at this?
Comment 3 Ito Kazumitsu 2006-09-18 11:16:52 UTC
I have confirmed that this problem is caused by the regex engine.

Running the following simple program with the command like
"java L 3000" will show the problem:

import java.util.regex.*;
public class L {
  public static void main(String[] args) throws Exception {
    Pattern p = Pattern.compile("(b)+");
    int n = Integer.parseInt(args[0]);
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < n; i++) {
      sb.append("b");
    }
    Matcher m = p.matcher(sb);
    m.matches();
  }
}

gnu.java.util.regex.RETokenRepeated uses a recursive call to find
a match, and the longer the match is, the deeper the recursive call
goes.
Comment 4 Ito Kazumitsu 2006-09-18 22:28:49 UTC
Changed the Component and Summary.
Comment 5 cvs-commit@developer.classpath.org 2006-09-22 21:43:29 UTC
Subject: Bug 29047

CVSROOT:	/cvsroot/classpath
Module name:	classpath
Changes by:	Ito Kazumitsu <itokaz>	06/09/22 21:36:36

Modified files:
	.              : ChangeLog 
	gnu/java/util/regex: RETokenRepeated.java 

Log message:
	2006-09-22  Ito Kazumitsu  <kaz@maczuka.gcd.org>
	
		Fixes bug #29047
		* gnu/java/util/regex/RETokenRepeated.java
		(findMatch): Rewriten without using recursive calls,
		(FindMatchControlStack): New class,
		(FindMatchControl): New class,
		(TryAnotherResult): New class,
		(tryAnother): New method.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/classpath/ChangeLog?cvsroot=classpath&r1=1.8595&r2=1.8596
http://cvs.savannah.gnu.org/viewcvs/classpath/gnu/java/util/regex/RETokenRepeated.java?cvsroot=classpath&r1=1.2&r2=1.3



Comment 6 Ito Kazumitsu 2006-09-22 23:25:13 UTC
I have fixed the bug, and my simple test program now passes.  Elias, could you please test your case?  I cannot run it because my VM, Kaffe on FreeBSD, is not fully functional now.  
Comment 7 Elias Naur 2006-09-23 07:43:28 UTC
Yes, the stack overlfow is gone with Classpath CVS HEAD. Thanks alot!

 - elias