JDK-7006761 : Matcher.matches() has infinite loop
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.regex
  • Affected Version: 6
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2010-12-14
  • Updated: 2016-05-19
  • Resolved: 2016-05-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b119Fixed
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_11"
Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
See steps to reproduce and run the code snippet.  The thread will hang in an infinite loop.

The regular expression pattern is invalid as far as I know; I don't think the "?+" is correct.  I would think that Pattern.compile() should be throwing an exception.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run this simple 2-line code snippet (full class below).

Pattern pattern = Pattern.compile("(([0-9A-Z]+)([_]?+)*)*");
pattern.matcher("FOOOOO_BAAAR_FOOOOOOOOO_BA_ ").matches();


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Should not hang.
ACTUAL -
VM hangs in an infinite loop.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.regex.Pattern;

public class RegexTest {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("(([0-9A-Z]+)([_]?+)*)*");
        pattern.matcher("FOOOOO_BAAAR_FOOOOOOOOO_BA_ ").matches();
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Correct the regex.

Comments
EVALUATION It does not run into "infinite loop", the regex runs into an "exponential backtracking" because it never can match the space character at the end and its usages of "*" in regex keeps it backtracking. Try possessive quantitifer should help solve the problem. While this is really not a bug of the regex engine, it might be nice to have some optimization in engine to detect this kind of backtracking and eliminate the match at early stage.
18-03-2011