8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
authorigerasim
Wed, 16 Jul 2014 13:02:24 +0400
changeset 25523 c751d1010164
parent 25522 10d789df41bb
child 25525 4ef885b4c7c6
8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException Reviewed-by: sherman
jdk/src/share/classes/java/util/regex/Pattern.java
jdk/test/java/util/regex/RegExTest.java
--- a/jdk/src/share/classes/java/util/regex/Pattern.java	Tue Jul 15 11:22:14 2014 -0700
+++ b/jdk/src/share/classes/java/util/regex/Pattern.java	Wed Jul 16 13:02:24 2014 +0400
@@ -917,6 +917,13 @@
      */
     public static final int UNICODE_CHARACTER_CLASS = 0x100;
 
+    /**
+     * Contains all possible flags for compile(regex, flags).
+     */
+    private static final int ALL_FLAGS = CASE_INSENSITIVE | MULTILINE |
+            DOTALL | UNICODE_CASE | CANON_EQ | UNIX_LINES | LITERAL |
+            UNICODE_CHARACTER_CLASS | COMMENTS;
+
     /* Pattern has only two serialized components: The pattern string
      * and the flags, which are all that is needed to recompile the pattern
      * when it is deserialized.
@@ -1336,6 +1343,10 @@
      * only a Start node and a LastNode node.
      */
     private Pattern(String p, int f) {
+        if ((f & ~ALL_FLAGS) != 0) {
+            throw new IllegalArgumentException("Unknown flag 0x"
+                                               + Integer.toHexString(f));
+        }
         pattern = p;
         flags = f;
 
--- a/jdk/test/java/util/regex/RegExTest.java	Tue Jul 15 11:22:14 2014 -0700
+++ b/jdk/test/java/util/regex/RegExTest.java	Wed Jul 16 13:02:24 2014 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -32,7 +32,7 @@
  * 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133
  * 6350801 6676425 6878475 6919132 6931676 6948903 6990617 7014645 7039066
  * 7067045 7014640 7189363 8007395 8013252 8013254 8012646 8023647 6559590
- * 8027645 8035076 8039124
+ * 8027645 8035076 8039124 8035975
  */
 
 import java.util.regex.*;
@@ -150,6 +150,7 @@
         groupCurlyNotFoundSuppTest();
         groupCurlyBackoffTest();
         patternAsPredicate();
+        invalidFlags();
 
         if (failure) {
             throw new
@@ -4457,4 +4458,30 @@
         }
         report("Pattern.asPredicate");
     }
+
+    // This test is for 8035975
+    private static void invalidFlags() throws Exception {
+        for (int flag = 1; flag != 0; flag <<= 1) {
+            switch (flag) {
+            case Pattern.CASE_INSENSITIVE:
+            case Pattern.MULTILINE:
+            case Pattern.DOTALL:
+            case Pattern.UNICODE_CASE:
+            case Pattern.CANON_EQ:
+            case Pattern.UNIX_LINES:
+            case Pattern.LITERAL:
+            case Pattern.UNICODE_CHARACTER_CLASS:
+            case Pattern.COMMENTS:
+                // valid flag, continue
+                break;
+            default:
+                try {
+                    Pattern.compile(".", flag);
+                    failCount++;
+                } catch (IllegalArgumentException expected) {
+                }
+            }
+        }
+        report("Invalid compile flags");
+    }
 }