[cp-patches] [PATCH] Fix Matcher.find() infinite loop bug

Pekka Enberg penberg at kernel.org
Wed Mar 2 14:22:10 UTC 2011


This patch fixes a problem in Match.find() where the following piece of code
would enter an infinite loop:

    Pattern p = Pattern.compile("\uFFFF");
    Matcher m = p.matcher("hello, world");
    System.out.println(m.find());

Signed-off-by: Pekka Enberg <penberg at kernel.org>
---
 ChangeLog                    |    5 +++++
 java/util/regex/Matcher.java |    6 ++++++
 2 files changed, 11 insertions(+), 0 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 05aa794..2b9cb5c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2011-03-02  Pekka Enberg  <penberg at kernel.org>
+
+	* java/util/regex/Matcher:
+	(find): Make sure match is within input data limits.
+
 2011-02-22  Pekka Enberg  <penberg at kernel.org>
 
 	* java/util/HashMap:
diff --git a/java/util/regex/Matcher.java b/java/util/regex/Matcher.java
index be57471..86c4873 100644
--- a/java/util/regex/Matcher.java
+++ b/java/util/regex/Matcher.java
@@ -169,6 +169,12 @@ public final class Matcher implements MatchResult
     if (match != null)
       {
         int endIndex = match.getEndIndex();
+        // Is the match within input limits?
+        if (endIndex >= input.length())
+          {
+            match = null;
+            return false;
+          }
         // Are we stuck at the same position?
         if (!first && endIndex == position)
           {
-- 
1.7.1




More information about the Classpath-patches mailing list