I encountered an issue with java.util.Scanner when building build drop 62
of OpenJDK7, caused by changes in character set generation:

http://download.java.net/jdk7/changes/jdk7-b62.html

When reading a file, hasNextLine() should return false when the end of the file
is reached.  However, in our implementation, this relies on myCoreNext() 
returning
null which it never does; when the Matcher hits the end, tmp2, the returned 
value,
is set to a substring of the input, starting at the current position.  At the 
end
of the file, this will be an empty string, not null.  This patch simply corrects
this, so that if the substring is empty, tmp2 is set to null.

It also fixes a bug in Matcher which was masking this error; when cloning the 
Matcher
for a read-only MatchResult object, we don't check that match is non-null.  
match is
only non-null if a match has been performed, so it is possible to create a 
Matcher,
call toMatchResult and elicit a NullPointerException.

ChangeLog:

2009-07-07  Andrew John Hughes  <ahug...@redhat.com>

        PR classpath/40630
        * java/util/Scanner.java:
        (myCoreNext(boolean, Pattern)): Set tmp2 to
        null if the string is empty (i.e. we are at
        the end of the file).
        * java/util/regex/Matcher.java:
        (toMatchResult()): Check that match is non-null
        before attempting to clone it.

-- 
Andrew :)

Free Java Software Engineer
Red Hat, Inc. (http://www.redhat.com)

Support Free Java!
Contribute to GNU Classpath and the OpenJDK
http://www.gnu.org/software/classpath
http://openjdk.java.net
PGP Key: 94EFD9D8 (http://subkeys.pgp.net)
Fingerprint = F8EF F1EA 401E 2E60 15FA  7927 142C 2591 94EF D9D8
Index: java/util/Scanner.java
===================================================================
RCS file: /sources/classpath/classpath/java/util/Scanner.java,v
retrieving revision 1.2
diff -u -u -r1.2 Scanner.java
--- java/util/Scanner.java      7 Jul 2009 10:12:10 -0000       1.2
+++ java/util/Scanner.java      7 Jul 2009 10:15:40 -0000
@@ -1668,6 +1668,8 @@
       // the end of input is matched
       {
         tmp2 = this.actBuffer.substring (this.actPos);
+        if (tmp2.length() == 0)
+          tmp2 = null;
         this.lastNextPos = this.actBuffer.length ();
         if (delete)
           {
Index: java/util/regex/Matcher.java
===================================================================
RCS file: /sources/classpath/classpath/java/util/regex/Matcher.java,v
retrieving revision 1.24
diff -u -u -r1.24 Matcher.java
--- java/util/regex/Matcher.java        7 Jul 2009 10:12:10 -0000       1.24
+++ java/util/regex/Matcher.java        7 Jul 2009 10:15:41 -0000
@@ -603,7 +603,8 @@
   public MatchResult toMatchResult()
   {
     Matcher snapshot = new Matcher(pattern, input);
-    snapshot.match = (REMatch) match.clone();
+    if (match != null)
+      snapshot.match = (REMatch) match.clone();
     return snapshot;
   }
 

Reply via email to