JDK-6328855 : String: Matches hangs at short and easy Strings containing \r \n
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.regex
  • Affected Version: 5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2005-09-26
  • Updated: 2017-10-16
  • 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 :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Linux ncc-1701-e 2.6.11.4-21.9-smp #1 SMP Fri Aug 19 11:58:59 UTC 2005 i686 i686 i386 GNU/Linux SuSE Linux 9.3 (i586)
Linux dauntless 2.6.11 #5 Tue Mar 15 17:45:56 CET 2005 i686 Mobile Intel(R) Celeron(R) CPU 2.40GHz GenuineIntel GNU/Linux Gentoo Base System version 1.4.16
WinXP SP2

A DESCRIPTION OF THE PROBLEM :
There have been a couple of bugs filed about java.lang.String.matches hangs on specific expression. Always the only comment was that their expression is to comprehensive. I've found an example which proves that this is a bug that may occur at any time.

The expression (.*\n*)* works on most Strings but crashes on some. Adding \r* is a workaround but in my opinion not neccessary.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
I wasn't able to find out how the given String has to look like but matching the String "this little fine string lets\r\njava.lang.String.matches\r\ncrash\r\n(We don't know why but adding \r* to the regex makes it work again)" against "(.*\r*\n*)*" works while "(.*\n*)*" crashes

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
System Output as:
true|false
true|false
ACTUAL -
true
- HANGS -

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class MatchesCrash {
    
    public MatchesCrash() {
    }
    
    public static void main(String[] args) {
        String str="this little fine string lets\r\njava.lang.String.matches\r\ncrash\r\n(We don't know why but adding \r* to the regex makes it work again)";
        /* works */
        System.out.println(str.matches("(.*\r*\n*)*"));
        /* crashes */
        System.out.println(str.matches("(.*\n*)*"));
    }
    
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
adding the \r*

Comments
EVALUATION because construct ".*" does not match \r, so the (.*\n*)* runs into an exponential backtracking.
13-11-2006